package cc.rengu.igas.mcps.core.service.trans.scanpay;

import cc.rengu.igas.channel.alipay.facade.bean.TradeFundBill;
import cc.rengu.igas.channel.alipay.facade.request.AliCloseOrderRequest;
import cc.rengu.igas.channel.alipay.facade.request.AliQueryOrderRequest;
import cc.rengu.igas.channel.alipay.facade.request.AliRefundQueryRequest;
import cc.rengu.igas.channel.alipay.facade.response.AliQueryOrderResponse;
import cc.rengu.igas.channel.alipay.facade.response.AliRefundQueryResponse;
import cc.rengu.igas.channel.upqc.facade.bean.CouponInfoBean;
import cc.rengu.igas.channel.upqc.facade.bean.PayerInfo;
import cc.rengu.igas.channel.upqc.facade.request.B2CTranStateQueryRequest;
import cc.rengu.igas.channel.upqc.facade.request.C2BTranStateQueryRequest;
import cc.rengu.igas.channel.upqc.facade.response.B2CTranStateQueryResponse;
import cc.rengu.igas.channel.upqc.facade.response.C2BTranStateQueryResponse;
import cc.rengu.igas.channel.wechat.facade.request.WechatCloseOrderRequest;
import cc.rengu.igas.channel.wechat.facade.request.WechatQueryOrderRequest;
import cc.rengu.igas.channel.wechat.facade.request.WechatQuerySingleRefundRequest;
import cc.rengu.igas.channel.wechat.facade.response.WechatQueryOrderResponse;
import cc.rengu.igas.channel.wechat.facade.response.WechatQuerySingleRefundResponse;
import cc.rengu.igas.mcps.common.constant.McpsParamConstant;
import cc.rengu.igas.mcps.common.constant.McpsServiceConstant;
import cc.rengu.igas.mcps.common.constant.McpsTreeNodeConstant;
import cc.rengu.igas.mcps.common.constant.McpsTxnNumConstant;
import cc.rengu.igas.mcps.common.enums.RespCodeEnum;
import cc.rengu.igas.mcps.core.realize.EsbRealSttlePaymentService;
import cc.rengu.igas.mcps.core.realize.impl.EsbRealSttlePaymentServiceImpl;
import cc.rengu.igas.mcps.core.service.base.B2CQueryService;
import cc.rengu.igas.mcps.facade.base.Header;
import cc.rengu.igas.mcps.facade.bean.OrderInfoBean;
import cc.rengu.igas.mcps.facade.bean.PayTypeInfo;
import cc.rengu.igas.mcps.facade.request.PosScanQrCodeStatusQueryRequest;
import cc.rengu.igas.mcps.facade.response.PosScanQrCodeStatusQueryResponse;
import cc.rengu.igas.share.common.constant.TransCacheConstant;
import cc.rengu.igas.share.core.model.MchntAcctInfo;
import cc.rengu.igas.share.core.model.MchntFeeInfo;
import cc.rengu.igas.share.core.model.MchntInfo;
import cc.rengu.igas.share.core.realize.MchntService;
import cc.rengu.igas.share.core.realize.impl.MchntServiceImpl;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.MchntOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.PayOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.UserOrderInfoMapper;
import cc.rengu.oltp.service.common.dao.impl.MchntOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.PayOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.dao.impl.UserOrderInfoMapperImpl;
import cc.rengu.oltp.service.common.entity.MchntOrderInfo;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.entity.SysParam;
import cc.rengu.oltp.service.common.entity.UserOrderInfo;
import cc.rengu.oltp.service.common.enums.*;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.OrderService;
import cc.rengu.oltp.service.realize.SysParamService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.service.realize.impl.OrderServiceImpl;
import cc.rengu.oltp.service.realize.impl.SysParamServiceImpl;
import cc.rengu.oltp.utility.util.*;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 条码和扫码交易结果查询
 * modify by fuyp on 20200311.
 */
public class PosScanQrCodeStatusQueryService extends B2CQueryService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest = new PosScanQrCodeStatusQueryRequest();
        ConvertUtil.convertOutput(posScanQrCodeStatusQueryRequest);
        return posScanQrCodeStatusQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        BizResponse<PosScanQrCodeStatusQueryResponse> bizResponse = new BizResponse<>();
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest = (PosScanQrCodeStatusQueryRequest) request;
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        /*获取商户基本信息*/
        MchntService mchntService = new MchntServiceImpl();
        MchntInfo mchntInfo = mchntService.getMchntInfo(posScanQrCodeStatusQueryRequest.getHeader().getInstId(), posScanQrCodeStatusQueryRequest.getMchntNo());
        if (null == mchntInfo) {
            rglog.error("查询商户基本信息失败<{}>", posScanQrCodeStatusQueryRequest.getMchntNo());
            throw new BizException(RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        /*定义返回报文*/
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = new PosScanQrCodeStatusQueryResponse();
        /* 判断查询商户订单号、流水号是否上送，如果未送返回交易*/
        String oriTraceNo = posScanQrCodeStatusQueryRequest.getOriTraceNo();
        String oriPayId = posScanQrCodeStatusQueryRequest.getOriPayId();
        if (null == oriTraceNo && null == oriPayId) {
            rglog.error("交易报文未上送商户订单号或者流水号");
            throw new BizException(RespCodeEnum.POS_FORMAT_ERROR.getRespCode(), RespCodeEnum.POS_FORMAT_ERROR.getRespDesc());
        }
        /*查询用户订单表，商户订单信息，支付订单信息表*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        /*原系统标识*/
        String msgSrcId = posScanQrCodeStatusQueryRequest.getHeader().getSrcSysId();
        /*交易渠道标识*/
        String transChannelId = posScanQrCodeStatusQueryRequest.getHeader().getChanlId();
        /*订单日期（如果在日切点，可能会造成，输入当天日期，可能查不到用户订单信息，日期要递减一天）*/
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        UserOrderInfo oriuserOrderInfo;
        if (oriTraceNo != null) {
            oriuserOrderInfo = userOrderInfoMapper.selectUserOrderInfoByFrontSeqNum(instId, oriTraceNo, msgSrcId, transChannelId);
            if (null == oriuserOrderInfo) {
                /*如果还没查到用户订单表信息，返回拒绝交易*/
                oriuserOrderInfo = userOrderInfoMapper.selectUserOrderInfoByFrontSeqNum(instId, oriTraceNo, msgSrcId, "*");
                if (null == oriuserOrderInfo) {
                    rglog.error("原交易流水不存在");
                    throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
                }
            }
        } else {
            /*原用户流水号,发起查证的系统和下单的系统同一系统*/
            oriuserOrderInfo = userOrderInfoMapper.selectUserOrderInfoByUserOrderId(instId, oriPayId);
            if (null == oriuserOrderInfo) {
                /*如果还没查到用户订单表信息，返回拒绝交易*/
                rglog.error("原交易流水不存在");
                throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
            }
        }

        /*原用户订单存内部树*/
        GlobalTransUtil.setCacheValue(TransCacheConstant.ORIG_USER_ORDER_INFO, oriuserOrderInfo);
        /*查询商户订单表表信息*/
        MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
        MchntOrderInfo orimchntOrderInfo = mchntOrderInfoMapper.selectMchntOrderInfoByPrimaryKey(instId, oriuserOrderInfo.getSysSeqNum(), posScanQrCodeStatusQueryRequest.getMchntNo());
        /*查询支付订单表,查询调用三方的支付订单表*/
        PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
        /*获取原交易支付订单号*/
        List<PayOrderInfo> payOrderInfoList = payOrderInfoMapper.selectPayOrderInfoBySysSeqNum(instId, oriuserOrderInfo.getSysSeqNum());
        /*如果查询支付订单表可能为空，三方发起查询时，用户还没有支付，并未产生支付订单表，
        如果查询一直是支付订单不存在，判断用户订单失效时间，如果时间失效，置失效时间延后10秒缓冲，把用户订单和商户订单置失效*/
        OrderInfoBean orderInfo = new OrderInfoBean();
        if (null == payOrderInfoList || payOrderInfoList.isEmpty()) {
            rglog.debug("原用户订单信息：<{}>", JSON.toJSONString(oriuserOrderInfo));
            /*如果商户超限等设置用户、商户订单表失败*/
            if ("2".equals(oriuserOrderInfo.getTransStatus())) {
                /*组返回报文*/
                BeanUtil.beanCopy(oriuserOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                bizResponse.setRespCode(RespCodeEnum.ORDER_EXPR_ERROR.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.ORDER_EXPR_ERROR.getRespDesc());
                return bizResponse;
            } else if ("0".equals(oriuserOrderInfo.getTransStatus()) &&
                    (BizStatusEnum.INIT.getStatus().equals(oriuserOrderInfo.getBizStatus())
                            || BizStatusEnum.PROCESSING.getStatus().equals(oriuserOrderInfo.getBizStatus()))) {
                /*判断订单是否超时,在订单失效时间基础上增加10秒*/
                String OrderExpiryTime = DateUtil.getDateByTimeStamp(DateUtil.getTimeStamp(oriuserOrderInfo.getOrderExpiryTime(), "yyyyMMddHHmmss") + 10000L, "yyyyMMddHHmmss");
                if (DateUtil.compareTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"), OrderExpiryTime, "yyyyMMddHHmmss") > 0) {
                    /*原订单失效，判断订单状态是否还是处理中，如果是处理中更新订单为失效状态和失败状态*/
                    /*更新原用户、商户订单失效*/
                    oriuserOrderInfo.setOrderStatus(OrderStatusEnum.INVALID.getStatus());
                    oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                    oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                    orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    OrderService orderService = new OrderServiceImpl();
                    List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                    mchntOrderInfoList.add(orimchntOrderInfo);
                    orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, null);
                    /*返回订单失败*/
                    BeanUtil.beanCopy(oriuserOrderInfo, orderInfo);
                    posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                    bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                    bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                    bizResponse.setRespCode(RespCodeEnum.ORDER_EXPR_ERROR.getRespCode());
                    bizResponse.setRespDesc(RespCodeEnum.ORDER_EXPR_ERROR.getRespDesc());
                    return bizResponse;
                } else {
                    /*返回订单处理中需要前端继续发起查询*/
                    BeanUtil.beanCopy(oriuserOrderInfo, orderInfo);
                    posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                    bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                    bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                    bizResponse.setRespCode(RespCodeEnum.USER_PAYING.getRespCode());
                    bizResponse.setRespDesc(RespCodeEnum.USER_PAYING.getRespDesc());
                    return bizResponse;
                }
            } else {
                rglog.error("原用户订单状态异常");
                throw new BizException(RespCodeEnum.POS_ORIG_ORDER_STATUS_ERROR.getRespCode(), RespCodeEnum.POS_ORIG_ORDER_STATUS_ERROR.getRespDesc());
            }
        }
        /*订单处理成功的,交易状态和订单状态判断*/
        if ("1".equals(oriuserOrderInfo.getTransStatus()) &&
                "03".equals(oriuserOrderInfo.getOrderStatus())) {
            if ("00".equals(oriuserOrderInfo.getBizStatus())) {
                /*成功的处理*/
                /*取支付订单表支付通道对应的支付订单状态，加成功状态校验*/
                payOrderInfoList = payOrderInfoList.stream().filter(item -> item.getMsgDstId().equals(ChannelEnum.UPWX.getChannelType()) ||
                        item.getMsgDstId().equals(ChannelEnum.NUWX.getChannelType()) || item.getMsgDstId().equals(ChannelEnum.UPAL.getChannelType())
                        || item.getMsgDstId().equals(ChannelEnum.NUAL.getChannelType()) || item.getMsgDstId().equals(ChannelEnum.UPQC.getChannelType())
                        || item.getMsgDstId().equals(ChannelEnum.WXZF.getChannelType()) || item.getMsgDstId().equals(ChannelEnum.ALIP.getChannelType())
                        || item.getMsgDstId().equals(ChannelEnum.ESB.getChannelType()) || item.getMsgDstId().equals(ChannelEnum.UPAC.getChannelType())).collect(Collectors.toList());
                if (null == payOrderInfoList || payOrderInfoList.isEmpty()) {
                    rglog.error("原支付订单状态异常,流水号:{}", oriTraceNo);
                    throw new BizException(RespCodeEnum.POS_ORIG_ORDER_STATUS_ERROR.getRespCode(), RespCodeEnum.POS_ORIG_ORDER_STATUS_ERROR.getRespDesc());
                }
                payOrderInfoList = payOrderInfoList.stream().filter(item -> "1".equals(item.getTransStatus()) && "00".equals(item.getBizStatus())).collect(Collectors.toList());
                if (null == payOrderInfoList || payOrderInfoList.isEmpty()) {
                    rglog.error("原支付订单状态异常,流水号:{}", oriTraceNo);
                    throw new BizException(RespCodeEnum.POS_ORIG_ORDER_STATUS_ERROR.getRespCode(), RespCodeEnum.POS_ORIG_ORDER_STATUS_ERROR.getRespDesc());
                }
                /*通过过滤可以可以找到唯一通道标识的支付订单表*/
            }
            /*TODO 设置返回报文*/
            PayTypeInfo payTypeInfo = convPayTypeInfo(payOrderInfoList.get(0));
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(payOrderInfoList.get(0), orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            rglog.debug("打印返回信息：<{}>", JSON.toJSONString(posScanQrCodeStatusQueryResponse));
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        /*订单处理失败*/
        if ("2".equals(oriuserOrderInfo.getTransStatus())) {
            /*组返回报文*/
            PayTypeInfo payTypeInfo = convPayTypeInfo(payOrderInfoList.get(0));
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(payOrderInfoList.get(0), orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        /*订单处理中*/
        if (("0".equals(oriuserOrderInfo.getTransStatus()) || "1".equals(oriuserOrderInfo.getTransStatus())
                || "3".equals(oriuserOrderInfo.getTransStatus())) && ("01".equals(oriuserOrderInfo.getBizStatus()) || BizStatusEnum.INIT.getStatus().equals(oriuserOrderInfo.getBizStatus()))) {
            /*取支付通道标识下对应的支付订单信息*/
            payOrderInfoList = payOrderInfoList.stream().filter(item -> item.getMsgDstId().equals(ChannelEnum.UPWX.getChannelType()) ||
                    item.getMsgDstId().equals(ChannelEnum.NUWX.getChannelType()) || item.getMsgDstId().equals(ChannelEnum.UPAL.getChannelType())
                    || item.getMsgDstId().equals(ChannelEnum.NUAL.getChannelType()) || item.getMsgDstId().equals(ChannelEnum.UPQC.getChannelType())
                    || item.getMsgDstId().equals(ChannelEnum.WXZF.getChannelType()) || item.getMsgDstId().equals(ChannelEnum.ALIP.getChannelType())
                    || item.getMsgDstId().equals(ChannelEnum.ESB.getChannelType()) || item.getMsgDstId().equals(ChannelEnum.UPAC.getChannelType())).collect(Collectors.toList());
            /*取交易状态0,1,3*/
            payOrderInfoList = payOrderInfoList.stream().filter(item -> "0".equals(item.getTransStatus()) || "3".equals(item.getTransStatus()) || "1".equals(item.getTransStatus())).collect(Collectors.toList());
            if (payOrderInfoList.isEmpty()) {
                /*订单状态异常*/
                rglog.error("原支付订单状态异常");
                throw new BizException(RespCodeEnum.POS_ORIG_ORDER_STATUS_ERROR.getRespCode(), RespCodeEnum.POS_ORIG_ORDER_STATUS_ERROR.getRespDesc());
            }

            /*取业务状态为01*/
            payOrderInfoList = payOrderInfoList.stream().filter(item -> "01".equals(item.getBizStatus()) || item.getBizStatus().equals(BizStatusEnum.INIT.getStatus())).collect(Collectors.toList());
            if (payOrderInfoList.isEmpty()) {
                /*订单状态异常*/
                rglog.error("原支付订单状态异常");
                throw new BizException(RespCodeEnum.POS_ORIG_ORDER_STATUS_ERROR.getRespCode(), RespCodeEnum.POS_ORIG_ORDER_STATUS_ERROR.getRespDesc());
            }
            if (BizStatusEnum.INIT.getStatus().equals(oriuserOrderInfo.getBizStatus())) {
                rglog.debug("订单状态<{}>", BizStatusEnum.INIT.getStatus());
                /*TODO 设置返回报文，支付订单初始状态继续查询*/
                PayTypeInfo payTypeInfo = convPayTypeInfo(payOrderInfoList.get(0));
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(payOrderInfoList.get(0), orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }
            /* 通过过滤可以找到唯一通道的标示*/
            PayOrderInfo oripayOrderInfo = payOrderInfoList.get(0);

            /*获取原交易对应的业务产品代码，查询商户订单表中的业务产品代码*/
            /*查询结算账号*/
            MchntAcctInfo mchntAcctInfo = getMchntAcctInfo(posScanQrCodeStatusQueryRequest, mchntInfo);
            /*内部树赋值*/
            xmlTreeUtil.setXmlTreeObjectValue(McpsTreeNodeConstant.MCHNT_ACCT_INFO, mchntAcctInfo);
            /*设置内部树赋值*/
            GlobalTransUtil.setCacheValue(TransCacheConstant.ORIG_PAY_ORDER_INFO, oripayOrderInfo);
            rglog.debug("打印原支付订单表的交易通道标识<{}>", oripayOrderInfo.getMsgDstId());
            if (ChannelEnum.ESB.getChannelType().equals(oripayOrderInfo.getMsgDstId())) {
                /*本带本扫码查询和银联无跳转查询*/
                if (ChannelEnum.ESB.getChannelType().equals(oripayOrderInfo.getMsgDstId())) {
                    rglog.debug("本贷本交易查证");
                    /*调用esb通道查询*/
                    if ("01".equals(TransTypeEnum.getTransType(oripayOrderInfo.getTxnNum()).getTransType())) {
                        /* 支付查证*/
                        /*根据原支付订单表中的卡类型判断借贷记*/
                        if ("01".equals(oripayOrderInfo.getPayerAcctType())) {
                            /*本行贷记卡支付查询*/
                            rglog.debug("本行贷记卡支付查询");
                            bizResponse = esbCreditScanQrCodeStatusQueryService(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, orimchntOrderInfo, oripayOrderInfo, mchntInfo);
                        } else {
                            /*本行借记卡支付查询*/
                            bizResponse = esbDebitScanQrCodeStatusQueryService(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, orimchntOrderInfo, oripayOrderInfo, mchntInfo);
                        }

                    } else if ("04".equals(TransTypeEnum.getTransType(oripayOrderInfo.getTxnNum()).getTransType())) {
                        /* 退款查证*/
                        bizResponse = esbRefundStatusQueryService(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, orimchntOrderInfo, oripayOrderInfo, mchntInfo);
                    } else {
                        /*不支持此类型的查询交易*/
                        throw new BizException(RespCodeEnum.POS_NOT_SUPPORT.getRespCode(), RespCodeEnum.POS_NOT_SUPPORT.getRespDesc());
                    }

                }
            } else {
                /*统一调关单交易，判断原交易支付订单表中通道交易码是否存储*/
                if (AppParamConstant.DEFAULT_BIZ_TYPE.equals(oripayOrderInfo.getBizType()) || "01".equals(oripayOrderInfo.getBizType())) {
                    /* 调用关单交易,原交易是退款的不需要关单 原交易码包含04代表退款*/
                    if (!"04".equals(TransTypeEnum.getTransType(oripayOrderInfo.getTxnNum()).getTransType())) {
                        closeOrder(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, oripayOrderInfo);
                    }
                }
                if (ChannelEnum.UPWX.getChannelType().equals(oripayOrderInfo.getMsgDstId()) || ChannelEnum.WXZF.getChannelType().equals(oripayOrderInfo.getMsgDstId())) {

                    /*调用微信通道查询*/
                    if ("01".equals(TransTypeEnum.getTransType(oripayOrderInfo.getTxnNum()).getTransType())) {
                        /* 支付查证*/
                        bizResponse = wechatScanQrCodeStatusQueryService(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, orimchntOrderInfo, oripayOrderInfo, mchntInfo);
                    } else if ("04".equals(TransTypeEnum.getTransType(oripayOrderInfo.getTxnNum()).getTransType())) {
                        /* 退款查证*/
                        bizResponse = wechatRefundStatusQueryService(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, orimchntOrderInfo, oripayOrderInfo, mchntInfo);
                    } else {
                        /*不支持此类型的查询交易*/
                        throw new BizException(RespCodeEnum.POS_NOT_SUPPORT.getRespCode(), RespCodeEnum.POS_NOT_SUPPORT.getRespDesc());
                    }

                } else if ("UPAL".equals(oripayOrderInfo.getMsgDstId()) || "ALIP".equals(oripayOrderInfo.getMsgDstId())) {

                    if ("01".equals(TransTypeEnum.getTransType(oripayOrderInfo.getTxnNum()).getTransType())) {
                        /*调用支付宝通道查询*/
                        bizResponse = aliPayScanQrCodeStatusQueryService(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, orimchntOrderInfo, oripayOrderInfo, mchntInfo);
                    } else if ("04".equals(TransTypeEnum.getTransType(oripayOrderInfo.getTxnNum()).getTransType())) {
                        /*退款查证*/
                        bizResponse = aliPayRefundStatusQueryService(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, orimchntOrderInfo, oripayOrderInfo, mchntInfo);
                    } else {
                        /*不支持此类型的查询交易*/
                        throw new BizException(RespCodeEnum.POS_NOT_SUPPORT.getRespCode(), RespCodeEnum.POS_NOT_SUPPORT.getRespDesc());
                    }

                } else {
                    if ("01".equals(TransTypeEnum.getTransType(oripayOrderInfo.getTxnNum()).getTransType())) {
                        if ("P01MC006".equals(orimchntOrderInfo.getTxnNum())) {
                            /*银联条码查询*/
                            bizResponse = C2BScanQrCodeStatusQueryService(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, orimchntOrderInfo, oripayOrderInfo, mchntInfo);

                        } else {
                            /*银联扫码查询*/
                            bizResponse = B2CScanQrCodeStatusQueryService(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, orimchntOrderInfo, oripayOrderInfo, mchntInfo);
                        }
                        /*银联二维码通道查询*/

                    } else if ("04".equals(TransTypeEnum.getTransType(oripayOrderInfo.getTxnNum()).getTransType())) {
                        if ("P01MC006".equals(orimchntOrderInfo.getTxnNum())) {
                            /*银联条码退款查询*/
                            bizResponse = B2CRefundStatusQueryService(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, orimchntOrderInfo, oripayOrderInfo, mchntInfo);

                        } else {
                            /*银联扫码退款查询*/
                            bizResponse = C2BRefundStatusQueryService(posScanQrCodeStatusQueryRequest, oriuserOrderInfo, orimchntOrderInfo, oripayOrderInfo, mchntInfo);
                        }
                    } else {
                        /*不支持此类型的查询交易*/
                        throw new BizException(RespCodeEnum.POS_NOT_SUPPORT.getRespCode(), RespCodeEnum.POS_NOT_SUPPORT.getRespDesc());
                    }
                }
            }
        }
        return bizResponse;
    }

    private void closeOrder(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                            UserOrderInfo oriuserOrderInfo, PayOrderInfo oripayOrderInfo) throws Exception {
        /*机构号、商户号*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        String mchntNo = posScanQrCodeStatusQueryRequest.getMchntNo();
        DubboService dubboService = new DubboServiceImpl();
        /*首先判断订单时间是否超时，如果超时，先调关单交易*/
        /*订单超时先进行关单交易，无论关单成功与否，都要去通道查询订单状态，已查询返回结果为准*/
        if (DateUtil.compareTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"),
                oriuserOrderInfo.getOrderExpiryTime(), "yyyyMMddHHmmss") > 0) {
            if (AppParamConstant.DEFAULT_BIZ_TYPE.equals(oripayOrderInfo.getBizType())) {
                /*微信关单*/
                WechatCloseOrderRequest wechatCloseOrderRequest = new WechatCloseOrderRequest();
                cc.rengu.igas.channel.wechat.facade.base.Header header = new cc.rengu.igas.channel.wechat.facade.base.Header();
                BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), header);
                header.setTxnNum(McpsTxnNumConstant.WECHAT_CLOSE);
                wechatCloseOrderRequest.setHeader(header);
                /*设置微信通道标识*/
                wechatCloseOrderRequest.setChannelId(oripayOrderInfo.getMsgDstId());
                MchntService mchntService = new MchntServiceImpl();
                MchntInfo mchntInfo = mchntService.getMchntInfo(instId, mchntNo);
                /* 设置通道类型*/
                wechatCloseOrderRequest.setChannelType(oripayOrderInfo.getDstChannelType());
                wechatCloseOrderRequest.setOrderId(oriuserOrderInfo.getSysSeqNum());
                wechatCloseOrderRequest.setThirdMchntNo(mchntInfo.getWechatMchntNo());
                /*网联idc标识*/
                wechatCloseOrderRequest.setIdcFlag(oripayOrderInfo.getRemark1());
                dubboService.callDubboService(McpsServiceConstant.WECHAT_SERVICE, McpsServiceConstant.WECHAT_CLOSE_ORDER_METHOD, wechatCloseOrderRequest);
            } else {
                /*支付宝关单接口*/
                AliCloseOrderRequest aliCloseOrderRequest = new AliCloseOrderRequest();
                cc.rengu.igas.channel.alipay.facade.base.Header header = new cc.rengu.igas.channel.alipay.facade.base.Header();
                BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), header);
                header.setTxnNum(McpsTxnNumConstant.ALIPAY_CLOSE);
                aliCloseOrderRequest.setHeader(header);
                /*设置支付宝关单通道标识*/
                aliCloseOrderRequest.setChannelId(oripayOrderInfo.getMsgDstId());
                /*设置通道类型针对（线上、线下、0扣率等渠道类型）*/
                aliCloseOrderRequest.setChannelType(oripayOrderInfo.getDstChannelType());
                /* 设置订单*/
                aliCloseOrderRequest.setOrderId(oriuserOrderInfo.getSysSeqNum());
                /*设置网联idc标识*/
                aliCloseOrderRequest.setIdcFlag(oripayOrderInfo.getRemark1());
                /* 获取商户信息 */
                MchntService mchntService = new MchntServiceImpl();
                MchntInfo mchntInfo = mchntService.getMchntInfo(instId, mchntNo);
                if (null != mchntInfo) {
                    aliCloseOrderRequest.setAppAuthToken(mchntInfo.getAlipayAppAuthToken());
                }
                dubboService.callDubboService(McpsServiceConstant.ALIPAY_SERVICE, McpsServiceConstant.ALIPAY_CLOSE_ORDER_METHOD, aliCloseOrderRequest);
            }
        }
    }

    /*微信查询交易*/
    BizResponse<PosScanQrCodeStatusQueryResponse> wechatScanQrCodeStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                                     UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                                     PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<PosScanQrCodeStatusQueryResponse> bizResponse = new BizResponse<>();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        /*创建相应报文*/
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = new PosScanQrCodeStatusQueryResponse();
        /*机构号、商户号*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        String mchntNo = posScanQrCodeStatusQueryRequest.getMchntNo();

        WechatQueryOrderRequest wechatQueryOrderRequest = new WechatQueryOrderRequest();
        /*引用通道包*/
        cc.rengu.igas.channel.wechat.facade.base.Header header = new cc.rengu.igas.channel.wechat.facade.base.Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), header);
        wechatQueryOrderRequest.setHeader(header);
        /*设置通道标识*/
        wechatQueryOrderRequest.setChannelId(oripayOrderInfo.getMsgDstId());
        /* 设置通道类型*/
        wechatQueryOrderRequest.setChannelType(oripayOrderInfo.getDstChannelType());
        if (ChannelEnum.UPWX.getChannelType().equals(wechatQueryOrderRequest.getChannelId())) {
            header.setTxnNum(McpsTxnNumConstant.CUPS_WECHAT_QUERY);
        } else {
            header.setTxnNum(McpsTxnNumConstant.WECHAT_QUERY);
        }

        /*设置微信子商户号*/
        if (ChannelEnum.UPWX.getChannelType().equals(wechatQueryOrderRequest.getChannelId())) {
            wechatQueryOrderRequest.setThirdMchntNo(mchntInfo.getWechatMchntNo());
        }

        /*设置原支付订单号*/
        wechatQueryOrderRequest.setOrderId(oriuserOrderInfo.getSysSeqNum());
        /*设置网联idc标识*/
        wechatQueryOrderRequest.setIdcFlag(oripayOrderInfo.getRemark1());
        DubboService dubboService = new DubboServiceImpl();
        cc.rengu.igas.channel.wechat.facade.result.Result<WechatQueryOrderResponse> wechatQueryOrderResponseResult =
                (cc.rengu.igas.channel.wechat.facade.result.Result) dubboService.callDubboService(
                        McpsServiceConstant.WECHAT_SERVICE, McpsServiceConstant.WECHAT_QUERYORDER_METHOD,
                        wechatQueryOrderRequest);
        if (null == wechatQueryOrderResponseResult || !wechatQueryOrderResponseResult.isSuccess() ||
                //add by lizhangtao 20210808 添加对空的判断，由于原交易状态通过TradeState判断，因此只需要不存在即可任务未知
                null == wechatQueryOrderResponseResult.getResult() ||
                null == wechatQueryOrderResponseResult.getResult().getRespCode() ||
                null == wechatQueryOrderResponseResult.getResult().getTradeState()) {
            /*调用dubbo失败，默认订单处理中*/
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        WechatQueryOrderResponse response = wechatQueryOrderResponseResult.getResult();
        oripayOrderInfo.setRespCode(response.getTradeState());
        /*设置支付通道返回流水号*/
        oripayOrderInfo.setChannelSeqNum(response.getChannelOrderId());
        /* 定义订单实现类*/
        OrderService orderService = new OrderServiceImpl();
        Header tmpHeader = new Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), tmpHeader);
        tmpHeader.setTraceNo(sysSeqNum);
        if ("00000000".equals(response.getRespCode())) {
            /*通讯层成功，判断微信给返业务层代码,业务层返回错误码*/
            if ("SUCCESS".equals(response.getTradeState())) {
                /* 交易结果成功,更新用户订单、商户订单、支付订单*/
                oripayOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oripayOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                /*保存金额*/
                //oripayOrderInfo.setRealPayAmt(response.getCashAmt());
                //oripayOrderInfo.setChannelDiscountAmt(response.getCouponAmt());
                /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                //通道优惠金额
                String channelDiscountAmt = "0.00";
                if (!StringUtil.isEmptyOrNull(response.getSettleAmt())) {
                    channelDiscountAmt = AmountUtil.subtraction(oriuserOrderInfo.getRealPayAmt(), response.getSettleAmt());
                }
                /*新增-记录通道优惠金额*/
                oripayOrderInfo.setChannelDiscountAmt(channelDiscountAmt);
                /*新增-实际清算本金=原实际清算本金(订单金额-对客营销金额,在对客营销中处理)-支付订单中通道优惠金额*/
                orimchntOrderInfo.setRealSettleAmt(AmountUtil.subtraction(orimchntOrderInfo.getRealSettleAmt(), channelDiscountAmt));
                /*根据返回卡类型判断支付类型*/
                if (!StringUtil.isEmptyOrNull(response.getBankType()) && response.getBankType().contains("DEBIT")) {
                    String localBankCardType = getSysParamCardType(instId, McpsParamConstant.NORMAL_CARDTYPE_WCHAT, McpsParamConstant.DEBIT);
                    if (!StringUtil.isEmptyOrNull(localBankCardType) && localBankCardType.equals(response.getBankType())) {
                        oripayOrderInfo.setPayerAcctType("00");
                    } else {
                        oripayOrderInfo.setPayerAcctType("10");
                    }

                } else if (!StringUtil.isEmptyOrNull(response.getBankType()) && response.getBankType().contains("CREDIT")) {
                    String localBankCardType = getSysParamCardType(instId, McpsParamConstant.NORMAL_CARDTYPE_WCHAT, "CREDIT");
                    if (!StringUtil.isEmptyOrNull(localBankCardType) && localBankCardType.equals(response.getBankType())) {
                        oripayOrderInfo.setPayerAcctType("01");
                    } else {
                        oripayOrderInfo.setPayerAcctType("11");
                    }

                } else {
                    oripayOrderInfo.setPayerAcctType("*");
                }
                /*付款人信息*/
                oripayOrderInfo.setPayerAcctInfo("微信用户");
                /*初始化服务*/
                MchntService mchntService = new MchntServiceImpl();
                if (!"0".equals(orimchntOrderInfo.getFeeInputMode())) {
                    /*调手续费计算接口并更新内部树原商户订单表*/
                    rglog.debug("手续费计算数据instId:<{}>,mchntNo<{}>,amount:<{}>,bizprodCode<{}>,feeType:<{}>,stageCode:<{}>,stageNum:<{}>", instId, mchntNo, response.getSettleAmt(), orimchntOrderInfo.getBizProdCode(), 04, null, null);
                    boolean calcResult = mchntService.calcMchntFee(orimchntOrderInfo, "04");
                    //mod by lizhangtao 由于商户费率信息表中不同的产品存以下三种组合：全特殊费率，全非特殊费率，特殊和非特殊都存在；
                    // 且计算手续费时只要存在特殊费率（不管是不是当前产品的）都不实时计算手续费
                    // 失败：
                    //   1. 返回为空 ||
                    //   2. 该商户计算手续费应答中认为非特殊费率商户（全都是非特殊费率），但是没有手续费
                    // 成功：(除以上失败)
                    //   返回对象非空 && ((返回的FeeInputMode为0 && 无手续费) || (返回的FeeInputMode为非0 && 有手续费))
                    if (!calcResult ||
                            (!"0".equals(orimchntOrderInfo.getFeeInputMode()) &&
                                    null == orimchntOrderInfo.getMchntFee())) {
                        rglog.error("计算商户手续费失败");
                        throw new BizException(RespCodeEnum.CALC_MCHNT_FEE_ERROR.getRespCode(), RespCodeEnum.CALC_MCHNT_FEE_ERROR.getRespDesc());
                    }
                    if ("0".equals(orimchntOrderInfo.getFeeInputMode())) {
                        rglog.debug("由于存在了其他产品为特殊计费，因此该商户<{}>被认为是特殊费率商户不计算手续费！！", orimchntOrderInfo.getMchntNo());
                        orimchntOrderInfo.setMchntSettleAmt(orimchntOrderInfo.getRealSettleAmt());
                    }
                } else {
                    rglog.debug("该商户<{}>被认为是特殊费率商户不计算手续费！！", orimchntOrderInfo.getMchntNo());
                    orimchntOrderInfo.setMchntSettleAmt(orimchntOrderInfo.getRealSettleAmt());
                }

                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新所有相关的订单表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                /*保存商户订单表于内部树*/
                xmlTreeUtil.setXmlTreeObjectValue(McpsTreeNodeConstant.MCHNT_ORDER_INFO, orimchntOrderInfo);
                /*
                 * 商户打款实时清算标志，商户是DO清算
                 * */
                MchntAcctInfo mchntAcctInfo = (MchntAcctInfo) xmlTreeUtil.getXmlTreeObjectValue(McpsTreeNodeConstant.MCHNT_ACCT_INFO, MchntAcctInfo.class);
                if ("99".equals(orimchntOrderInfo.getRealSettleType()) && ("00".equals(mchntAcctInfo.getSettleAcctType()) ||
                        "02".equals(mchntAcctInfo.getSettleAcctType()) || "03".equals(mchntAcctInfo.getSettleAcctType()))) {
                    /*判断清算本金是否小于实时入账最小金额*/
                    String minAmt = getRealTimeClearAmt(orimchntOrderInfo.getInstId());
                    /*判断交易金额是否大于实时入账最低金额*/
                    if (AmountUtil.compare(orimchntOrderInfo.getOrderAmt(), minAmt) != -1) {
                        /*调esb进行D0清算*/
                        EsbRealSttlePaymentService esbRealSttlePaymentService = new EsbRealSttlePaymentServiceImpl();
                        esbRealSttlePaymentService.esbRealSettlePayment(orimchntOrderInfo, oripayOrderInfo, posScanQrCodeStatusQueryRequest.getHeader());
                        orimchntOrderInfo.setRealSettleFlag(AppParamConstant.YES);
                    } else {
                        orimchntOrderInfo.setRealSettleFlag(AppParamConstant.NO);
                    }
                } else {
                    GlobalTransUtil.setCacheValue(TransCacheConstant.USER_ORDER_INFO, oriuserOrderInfo);
                    int iReturnCode = asynnotify.transactionNotice("MchntTransNoticeService", 1, 0, 0);
                    if (0 != iReturnCode) {
                        rglog.error("提交交易结果异步通知给商户失败!");
                    }
                }
                /* 累计商户限额*/
                mchntService.updateMchntTransLimit(orimchntOrderInfo);
                /*TODO  设置相应报文*/
                /*原交易的流水号*/
                /*TODO 设置返回报文*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            } else if ("USERPAYING".equals(response.getTradeState()) ||
                    "NOTPAY".equals(response.getTradeState())) {
                /*支付中处理*/
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                oripayOrderInfo.setBizStatus(BizStatusEnum.PROCESSING.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.TRANS_INIT.getStatus());
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                orderInfo.setBizStatus(oripayOrderInfo.getBizStatus());
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            } else {
                /*支付失败包括：未支付：NOTPAY 订单已关闭：CLOSED 已撤销：REVOKED 支付错误：PAYERROR 转入退款 REFUND*/
                if ("REVOKED".equals(response.getTradeState())) {
                    /*原交易已撤销的交易*/
                    /*用户订单订单状态完成，交易状态失败，业务状态已撤销*/
                    oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                    oriuserOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                    oriuserOrderInfo.setBizStatus(BizStatusEnum.CANCEL.getStatus());
                    /*商户订单表状态*/
                    orimchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                    orimchntOrderInfo.setBizStatus(BizStatusEnum.CANCEL.getStatus());
                    /*支付订单状态*/
                    oripayOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                    oripayOrderInfo.setBizStatus(BizStatusEnum.CANCEL.getStatus());

                } else {
                    /*交易失败的处理*/
                    /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                    oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                    oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                    oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    /*商户订单表交易状态 1，业务状态：01 交易完成*/
                    orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                    orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    /* 支付订单状态*/
                    oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                }
                /*原交易的流水号*/
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新所有相关的订单表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }
        } else {
            //del by lizhangtao 20210808 根据通道逻辑，不是00000000都是查询失败，即使不发起重发查询也是查询本身的失败，因此对于原交易都需要设置为超时未知
//            if (OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode().equals(response.getRespCode()) ||
//                    "ER12WX19".equals(response.getRespCode()) ||
//                    "SYSTEMERROR".equals(response.getRespCode())) {
            /*ER12WX19调微信发送服务失败*/
            /*状态未知(UP0226200)*/
            /*支付中处理*/
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
            //del by lizhangtao 20210808
//            } else {
//                /* 更新用户订单、商户订单、支付订单*/
//                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
//                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
//                /* 用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
//                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
//                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
//                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
//                /*商户订单表交易状态 1，业务状态：01 交易完成*/
//                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
//                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
//                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<MchntOrderInfo>();
//                mchntOrderInfoList.add(orimchntOrderInfo);
//                /*更新所有相关的订单表*/
//                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
//                OrderInfo orderInfo = new OrderInfo();
//                BeanUtil.beanCopy(oripayOrderInfo,orderInfo);
//                BeanUtil.nullValueCopy(oriuserOrderInfo,orderInfo);
//                BeanUtil.nullValueCopy(orimchntOrderInfo,orderInfo);
//                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
//                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
//                bizResponse.setRespCode(response.getRespCode());
//                bizResponse.setRespDesc(response.getRespDesc());
//                rglog.error("第三方服务返回业务结果不成功，respCode={},respDesc={}", response.getRespCode(), response.getRespDesc());
//                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
//                return bizResponse;
//            }
        }
    }

    /*支付宝查询交易*/
    BizResponse<PosScanQrCodeStatusQueryResponse> aliPayScanQrCodeStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                                     UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                                     PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<PosScanQrCodeStatusQueryResponse> bizResponse = new BizResponse<PosScanQrCodeStatusQueryResponse>();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        String mchntNo = posScanQrCodeStatusQueryRequest.getMchntNo();
        /*创建相应报文*/
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = new PosScanQrCodeStatusQueryResponse();
        /*机构号、商户号*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        AliQueryOrderRequest aliQueryOrderRequest = new AliQueryOrderRequest();
        cc.rengu.igas.channel.alipay.facade.base.Header header = new cc.rengu.igas.channel.alipay.facade.base.Header();
        /*复制报文头*/
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), header);
        header.setTxnNum(McpsTxnNumConstant.ALIPAY_QUERY);
        aliQueryOrderRequest.setHeader(header);
        /*通道标识*/
        aliQueryOrderRequest.setChannelId(oripayOrderInfo.getMsgDstId());
        /*通道类型*/
        aliQueryOrderRequest.setChannelType(oripayOrderInfo.getDstChannelType());
        aliQueryOrderRequest.setOrderId(oripayOrderInfo.getSysSeqNum());
        aliQueryOrderRequest.setIdcFlag(oripayOrderInfo.getRemark1());
        aliQueryOrderRequest.setAppAuthToken(mchntInfo.getAlipayAppAuthToken());
        DubboService dubboService = new DubboServiceImpl();
        cc.rengu.igas.channel.alipay.facade.result.Result<AliQueryOrderResponse> aliQueryOrderResponseResult =
                (cc.rengu.igas.channel.alipay.facade.result.Result) dubboService.callDubboService(
                        McpsServiceConstant.ALIPAY_SERVICE, McpsServiceConstant.ALIPAY_QUERYORDER_METHOD, aliQueryOrderRequest);
        if (null == aliQueryOrderResponseResult || !aliQueryOrderResponseResult.isSuccess() ||
                //add by lizhangtao 20210812 添加对空的判断，由于原交易状态通过TradeStatus判断，因此只需要不存在即可认为未知
                null == aliQueryOrderResponseResult.getResult() ||
                null == aliQueryOrderResponseResult.getResult().getRespCode() ||
                null == aliQueryOrderResponseResult.getResult().getTradeStatus()) {
            /*调用dubbo失败，默认订单处理中*/
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        AliQueryOrderResponse response = aliQueryOrderResponseResult.getResult();
        /*设置支付订单相应码和相应信息*/
        oripayOrderInfo.setRespCode(response.getTradeStatus());
        /*设置支付通道返回流水号*/
        oripayOrderInfo.setChannelSeqNum(response.getChannelOrderId());
        /*定义订单实现类*/
        OrderService orderService = new OrderServiceImpl();
        Header tmpHeader = new Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), tmpHeader);
        tmpHeader.setTraceNo(sysSeqNum);
        if ("00000000".equals(response.getRespCode())) {
            if ("TRADE_SUCCESS".equals(response.getTradeStatus()) || "TRADE_FINISHED".equals(response.getTradeStatus())) {
                //成功
                oripayOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                /*实收金额*/
                oripayOrderInfo.setRealPayAmt(response.getReceiptAmt());
                /*商家优惠金额*/
                oripayOrderInfo.setChannelDiscountAmt(response.getMerDiscAmt());
                /* 用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                //通道优惠金额
                String channelDiscountAmt = "0.00";
                if (!StringUtil.isEmptyOrNull(response.getSettleAmt())) {
                    channelDiscountAmt = AmountUtil.subtraction(oriuserOrderInfo.getRealPayAmt(), response.getSettleAmt());
                }
                /*新增-记录通道优惠金额*/
                oripayOrderInfo.setChannelDiscountAmt(channelDiscountAmt);
                /*新增-实际清算本金=原实际清算本金(订单金额-对客营销金额,在对客营销中处理)-支付订单中通道优惠金额*/
                orimchntOrderInfo.setRealSettleAmt(AmountUtil.subtraction(orimchntOrderInfo.getRealSettleAmt(), channelDiscountAmt));
                /*获取支付类型*/
                List<TradeFundBill> tradeFundBillList = response.getTradeFundBillList();
                if (null != tradeFundBillList && !tradeFundBillList.isEmpty()) {
                    TradeFundBill tradeFundBill = tradeFundBillList.get(0);
                    if (StringUtil.isEmptyOrNull(tradeFundBill.getFundType())) {
                        oripayOrderInfo.setPayerAcctType(AcctTypeEnum.ALL.getAcctType());
                    } else {
                        //fundType不为空
                        //fundType 前只在资金 渠道(fundChnl) 是 银 行 卡 渠 道 (BANKCARD)的情况下才返回该信息
                        if ("DEBIT_CARD".equals(tradeFundBill.getFundType())) {
                            //借记卡
                            oripayOrderInfo.setPayerAcctType(AcctTypeEnum.OTHER_DEBIT.getAcctType());
                        } else {
                            //CREDIT_CARD:信用卡 MIXED_CARD:借贷合一卡
                            oripayOrderInfo.setPayerAcctType(AcctTypeEnum.OTHER_CREDIT.getAcctType());
                        }
                    }
                }
                /*付款账户*/
                oripayOrderInfo.setPayerAcctInfo(response.getBuyerLogonId());
                /*初始化服务*/
                MchntService mchntService = new MchntServiceImpl();
                if (!"0".equals(orimchntOrderInfo.getFeeInputMode())) {
                    rglog.debug("手续费计算数据instId:<{}>,mchntNo<{}>,amount:<{}>,bizprodCode<{}>,feeType:<{}>,stageCode:<{}>,stageNum:<{}>", instId, mchntNo, response.getReceiptAmt(), orimchntOrderInfo.getBizProdCode(), 05, null, null);
                    boolean calcResult = mchntService.calcMchntFee(orimchntOrderInfo, "05");
                    //mod by lizhangtao 由于商户费率信息表中不同的产品存以下三种组合：全特殊费率，全非特殊费率，特殊和非特殊都存在；
                    // 且计算手续费时只要存在特殊费率（不管是不是当前产品的）都不实时计算手续费
                    // 失败：
                    //   1. 返回为空 ||
                    //   2. 该商户计算手续费应答中认为非特殊费率商户（全都是非特殊费率），但是没有手续费
                    // 成功：(除以上失败)
                    //   返回对象非空 && ((返回的FeeInputMode为0 && 无手续费) || (返回的FeeInputMode为非0 && 有手续费))
                    if (!calcResult ||
                            (!"0".equals(orimchntOrderInfo.getFeeInputMode()) &&
                                    null == orimchntOrderInfo.getMchntFee())) {
                        rglog.error("计算商户手续费失败");
                        throw new BizException(RespCodeEnum.CALC_MCHNT_FEE_ERROR.getRespCode(), RespCodeEnum.CALC_MCHNT_FEE_ERROR.getRespDesc());
                    }
                    if ("0".equals(orimchntOrderInfo.getFeeInputMode())) {
                        rglog.debug("由于存在了其他产品为特殊计费，因此该商户<{}>被认为是特殊费率商户不计算手续费！！", orimchntOrderInfo.getMchntNo());
                        orimchntOrderInfo.setMchntSettleAmt(orimchntOrderInfo.getRealSettleAmt());
                    }
                } else {
                    rglog.debug("该商户<{}>被认为是特殊费率商户不计算手续费！！", orimchntOrderInfo.getMchntNo());
                    orimchntOrderInfo.setMchntSettleAmt(orimchntOrderInfo.getRealSettleAmt());
                }

                /*计算手续费，更新商户订单表中的手续费字段,并且更新内部树*/
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新所有相关的订单表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                /*保存商户订单表于内部树，用于虚拟账户单笔记账用*/
                xmlTreeUtil.setXmlTreeObjectValue(McpsTreeNodeConstant.MCHNT_ORDER_INFO, orimchntOrderInfo);
                /*
                 * 判断商户是否是实时清算
                 * 商户打款实时清算标志，商户是DO清算
                 * */
                MchntAcctInfo mchntAcctInfo = (MchntAcctInfo) xmlTreeUtil.getXmlTreeObjectValue(McpsTreeNodeConstant.MCHNT_ACCT_INFO, MchntAcctInfo.class);
                if ("99".equals(orimchntOrderInfo.getRealSettleType()) && ("00".equals(mchntAcctInfo.getSettleAcctType()) ||
                        "02".equals(mchntAcctInfo.getSettleAcctType()) || "03".equals(mchntAcctInfo.getSettleAcctType()))) {
                    /*判断清算本金是否小于实时入账最小金额*/
                    String minAmt = getRealTimeClearAmt(orimchntOrderInfo.getInstId());
                    /*判断交易金额是否大于实时入账最低金额*/
                    if (AmountUtil.compare(orimchntOrderInfo.getOrderAmt(), minAmt) != -1) {
                        /*调esb进行D0清算*/
                        EsbRealSttlePaymentService esbRealSttlePaymentService = new EsbRealSttlePaymentServiceImpl();
                        esbRealSttlePaymentService.esbRealSettlePayment(orimchntOrderInfo, oripayOrderInfo, posScanQrCodeStatusQueryRequest.getHeader());
                        orimchntOrderInfo.setRealSettleFlag(AppParamConstant.YES);
                    } else {
                        orimchntOrderInfo.setRealSettleFlag(AppParamConstant.NO);
                    }
                } else {
                    GlobalTransUtil.setCacheValue(TransCacheConstant.USER_ORDER_INFO, oriuserOrderInfo);
                    int iReturnCode = asynnotify.transactionNotice("MchntTransNoticeService", 1, 0, 0);
                    if (0 != iReturnCode) {
                        rglog.error("提交交易结果异步通知给商户失败!");
                    }
                    /*调用微信推送*/
                }
                mchntService.updateMchntTransLimit(orimchntOrderInfo);
                /*TODO 设置返回报文*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            } else if ("WAIT_BUYER_PAY".equals(response.getTradeStatus())) {
                /*TODO  设置相应报文*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            } else {
                /*更新用户订单、商户订单、支付订单*/
                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新所有相关的订单表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                rglog.debug("第三方服务返回业务结果不成功，respCode={},respDesc={}", response.getRespCode(), response.getRespDesc());
                return bizResponse;
            }

        } else {
            //del by lizhangtao 20210812 根据通道逻辑，不是00000000都是查询失败，即使不发起重发查询也是查询本身的失败，因此对于原交易都需要设置为超时未知
//            if (OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode().equals(response.getRespCode()) ||
//                    "ER12WX19".equals(response.getRespCode())) {
            /*dubbo服务超时调用dubbo服务超时*/
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
            //del by lizhangtao 20210812
//            } else {
//                /*更新用户订单、商户订单、支付订单*/
//                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
//                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
//                /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
//                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
//                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
//                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
//                /*商户订单表交易状态 1，业务状态：01 交易完成*/
//                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
//                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
//                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
//                mchntOrderInfoList.add(orimchntOrderInfo);
//                /*更新所有相关的订单表*/
//                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
//                OrderInfo orderInfo = new OrderInfo();
//                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
//                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
//                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
//                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
//                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
//                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
//                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
//                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
//                rglog.debug("第三方服务返回业务结果不成功，respCode={},respDesc={}", response.getRespCode(), response.getRespDesc());
//                return bizResponse;
//            }
        }
    }

    /*
     * 云闪付条码支付送的地址是联机交易地址，
     * 支付结果是实时返回的，次查询交易处理的是云闪付扫码支付
     * */
    BizResponse<PosScanQrCodeStatusQueryResponse> C2BScanQrCodeStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                                  UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                                  PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<PosScanQrCodeStatusQueryResponse> bizResponse = new BizResponse<>();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        /*创建相应报文*/
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = new PosScanQrCodeStatusQueryResponse();
        /*机构号、商户号*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        C2BTranStateQueryRequest c2BTranStateQueryRequest = new C2BTranStateQueryRequest();
        cc.rengu.igas.channel.upqc.facade.base.Header header = new cc.rengu.igas.channel.upqc.facade.base.Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), header);
        /*判断原交易是条码交易还是扫码交易，通过交易码识别*/
        DubboService dubboService = new DubboServiceImpl();
        header.setTxnNum(McpsTxnNumConstant.C2B_UPQC_QUERY);
        c2BTranStateQueryRequest.setHeader(header);
        c2BTranStateQueryRequest.setCupsInstCode(mchntInfo.getCupsInstId());
        /*设置查询请求报文*/
        c2BTranStateQueryRequest.setOrderId(oripayOrderInfo.getSysSeqNum());
        c2BTranStateQueryRequest.setOrderTime(oripayOrderInfo.getTxnDate() + oripayOrderInfo.getTxnTime());
        rglog.debug("银联二维码条码支付查询请求报文{}", JSON.toJSONString(c2BTranStateQueryRequest));
        cc.rengu.igas.channel.upqc.facade.result.Result<C2BTranStateQueryResponse> c2BTranStateQueryResponseResult =
                (cc.rengu.igas.channel.upqc.facade.result.Result) dubboService.callDubboService(
                        McpsServiceConstant.UPQC_SERVICE, McpsServiceConstant.UPQC_C2BTRANSSTATEQUERY_METHOM, c2BTranStateQueryRequest);
        if (null == c2BTranStateQueryResponseResult || !c2BTranStateQueryResponseResult.isSuccess() ||
                //add by lizhangtao 20210812 添加对空的判断，由于原交易状态通过OrigRespCode判断，因此只需要不存在即可任务未知
                null == c2BTranStateQueryResponseResult.getResult() ||
                null == c2BTranStateQueryResponseResult.getResult().getRespCode() ||
                null == c2BTranStateQueryResponseResult.getResult().getOrigRespCode()) {
            /*调用dubbo失败，默认订单处理中*/
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        C2BTranStateQueryResponse response = c2BTranStateQueryResponseResult.getResult();
        /*设置支付订单相应码和相应信息*/
        oripayOrderInfo.setRespCode(response.getOrigRespCode());
        oripayOrderInfo.setRespDesc(response.getOrigRespDesc());
        /*设置银联返回的付款凭证号*/
        oripayOrderInfo.setChannelOrderId(response.getVoucherNum());
        /*定义订单实现类*/
        OrderService orderService = new OrderServiceImpl();
        MchntService mchntService = new MchntServiceImpl();
        Header tmpHeader = new Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), tmpHeader);
        tmpHeader.setTraceNo(sysSeqNum);
        if ("00000000".equals(response.getRespCode())) {
            if ("00".equals(response.getOrigRespCode())) {
                oripayOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                oripayOrderInfo.setChannelSeqNum(response.getSettleKey());
                oripayOrderInfo.setSettleDate(response.getSettleDate());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                /*成功,处理接受信息，首先判断发卡机构更新付款账户类型*/
                PayerInfo payInfo = response.getPayerInfo();
                /* 获取本行发卡机构号*/
                String issCode = getSysParamIssCode(instId);
                if (null == issCode) {
                    /*没有配置发卡行机构号，用户付款类型默认其他*/
                    /*默认他行卡*/
                    if ("01".equals(payInfo.getCardAttr())) {
                        oripayOrderInfo.setPayerAcctType("10");
                    } else {
                        oripayOrderInfo.setPayerAcctType("11");
                    }
                } else {
                    if ("01".equals(payInfo.getCardAttr())) {
                        /*借记卡，区分本行和他行借记卡*/
                        if (issCode.equals(payInfo.getIssCode())) {
                            oripayOrderInfo.setPayerAcctType("00");
                        } else {
                            oripayOrderInfo.setPayerAcctType("10");
                        }
                    } else {

                        if (issCode.equals(payInfo.getIssCode())) {
                            oripayOrderInfo.setPayerAcctType("01");
                        } else {
                            oripayOrderInfo.setPayerAcctType("11");
                        }
                    }
                }
                /*付款人信息*/
                oripayOrderInfo.setPayerAcctInfo(payInfo.getAccNo());
                /*  返回订单处理中 继续发起查询*/
                String bRegisterMompFlag = xmlTreeUtil.getXmlTreeStringValue("bRegisterMompFlag");
                if ("00000002".equals(bRegisterMompFlag)) {
                    /*支付中处理*/
                    OrderInfoBean orderInfo = new OrderInfoBean();
                    PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                    orderInfo.setPayTypeInfo(payTypeInfo);
                    BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                    BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                    BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                    posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                    bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                    bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                    bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                    bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                    return bizResponse;
                }
                oripayOrderInfo.setPayeeAcctInfo(payInfo.getName());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                //更新所有相关的订单表
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                /*保存商户订单表于内部树，用于虚拟账户单笔记账用*/
                xmlTreeUtil.setXmlTreeObjectValue(McpsTreeNodeConstant.MCHNT_ORDER_INFO, orimchntOrderInfo);
                /*
                 * 判断商户是否是实时清算,商户打款实时清算标志，商户是DO清算
                 * */
                MchntAcctInfo mchntAcctInfo = (MchntAcctInfo) xmlTreeUtil.getXmlTreeObjectValue(McpsTreeNodeConstant.MCHNT_ACCT_INFO, MchntAcctInfo.class);
                if ("99".equals(orimchntOrderInfo.getRealSettleType()) && ("00".equals(mchntAcctInfo.getSettleAcctType()) ||
                        "02".equals(mchntAcctInfo.getSettleAcctType()) || "03".equals(mchntAcctInfo.getSettleAcctType()))) {
                    /*判断清算本金是否小于实时入账最小金额*/
                    String minAmt = getRealTimeClearAmt(orimchntOrderInfo.getInstId());
                    /*判断交易金额是否大于实时入账最低金额*/
                    if (AmountUtil.compare(orimchntOrderInfo.getOrderAmt(), minAmt) != -1) {
                        /*调esb进行D0清算*/
                        EsbRealSttlePaymentService esbRealSttlePaymentService = new EsbRealSttlePaymentServiceImpl();
                        esbRealSttlePaymentService.esbRealSettlePayment(orimchntOrderInfo, oripayOrderInfo, posScanQrCodeStatusQueryRequest.getHeader());
                        orimchntOrderInfo.setRealSettleFlag(AppParamConstant.YES);
                    } else {
                        orimchntOrderInfo.setRealSettleFlag(AppParamConstant.NO);
                    }
                } else {
                    GlobalTransUtil.setCacheValue(TransCacheConstant.USER_ORDER_INFO, oriuserOrderInfo);
                    int iReturnCode = asynnotify.transactionNotice("MchntTransNoticeService", 1, 0, 0);
                    if (0 != iReturnCode) {
                        rglog.error("提交交易结果异步通知给商户失败!");
                    }
                    /*调用微信推送*/
                }
                mchntService.updateMchntTransLimit(orimchntOrderInfo);
                /*TODO 设置返回报文*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;

            } else if ("04".equals(response.getOrigRespCode()) || "06".equals(response.getOrigRespCode())) {
                /* 处理中交易，支付中状态，返回处理中判断订单有效时间是否超过有效时间，如果超过有效时间，把定单关闭*/
                if (DateUtil.compareTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"),
                        oriuserOrderInfo.getOrderExpiryTime(), "yyyyMMddHHmmss") > 0) {
                    /*更新用户订单、商户订单、支付订单*/
                    oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                    /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                    oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                    oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                    oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    /*商户订单表交易状态 1，业务状态：01 交易完成*/
                    orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                    orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                    mchntOrderInfoList.add(orimchntOrderInfo);
                    /*更新所有相关的订单表*/
                    orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                }
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            } else {
                /*失败交易*/
                /*更新用户订单、商户订单、支付订单*/
                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新所有相关的订单表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }

        }
        //原交易不存在或状态不正确,不能直接作为失败处理，存在查询比支付后发先至的问题
        else if ("35".equals(response.getRespCode())) {
            /*判断订单有效时间是否超过有效时间，如果超过有效时间，把定单关闭*/
            if (DateUtil.compareTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"),
                    oriuserOrderInfo.getOrderExpiryTime(), "yyyyMMddHHmmss") > 0) {
                /* TODO  订单超时定义失败 设置相应报文*/
                /*更新用户订单、商户订单、支付订单*/
                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                /* 用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新所有相关的订单表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
            }
            //没有超过订单有效时间则认为查询交易后发先至，后续继续确认状态
            else {
            }
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            return bizResponse;
        }
        //04交易状态未明，06系统繁忙请稍后确认
        else if ("04".equals(response.getRespCode()) || "06".equals(response.getRespCode())) {
            /* 处理中交易，支付中状态，返回处理中判断订单有效时间是否超过有效时间，如果超过有效时间，把定单关闭*/
            if (DateUtil.compareTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"),
                    oriuserOrderInfo.getOrderExpiryTime(), "yyyyMMddHHmmss") > 0) {
                /*TODO  设置相应报文*/
                /*更新用户订单、商户订单、支付订单*/
                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新所有相关的订单表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
            } else {
                /*TODO  设置相应报文*/
            }
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        //明确超时
        else if (response.getRespCode().equals(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode())) {
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        //系统内部失败，作为查询交易本身失败
        else if (response.getRespCode().equals(RespCodeEnum.POS_BIZ_SERVICE_ERROR.getRespCode())) {
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(RespCodeEnum.POS_BIZ_SERVICE_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.POS_BIZ_SERVICE_ERROR.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        } else {
            /*更新用户订单、商户订单、支付订单*/
            oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
            oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
            //用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成
            oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
            oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
            oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
            //商户订单表交易状态 1，业务状态：01 交易完成
            orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
            orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
            List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
            mchntOrderInfoList.add(orimchntOrderInfo);
            /*更新所有相关的订单表*/
            orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
    }


    BizResponse<PosScanQrCodeStatusQueryResponse> B2CScanQrCodeStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                                  UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                                  PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<PosScanQrCodeStatusQueryResponse> bizResponse = new BizResponse<>();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        /*创建相应报文*/
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = new PosScanQrCodeStatusQueryResponse();
        /*机构号、商户号*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        B2CTranStateQueryRequest b2CTranStateQueryRequest = new B2CTranStateQueryRequest();
        cc.rengu.igas.channel.upqc.facade.base.Header header = new cc.rengu.igas.channel.upqc.facade.base.Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), header);
        /*判断原交易是条码交易还是扫码交易，通过交易码识别*/
        DubboService dubboService = new DubboServiceImpl();
        /*条码查询*/
        header.setTxnNum(McpsTxnNumConstant.B2C_UPQC_QUERY);
        b2CTranStateQueryRequest.setHeader(header);
        b2CTranStateQueryRequest.setCupsInstCode(mchntInfo.getCupsInstId());
        /*设置查询请求报文*/
        b2CTranStateQueryRequest.setOrderId(oripayOrderInfo.getSysSeqNum());
        b2CTranStateQueryRequest.setOrderTime(oripayOrderInfo.getTxnDate() + oripayOrderInfo.getTxnTime());
        rglog.debug("银联二维码扫码支付查询请求报文{}", JSON.toJSONString(b2CTranStateQueryRequest));
        /*条码支付查询*/
        cc.rengu.igas.channel.upqc.facade.result.Result<B2CTranStateQueryResponse> b2CTranStateQueryResponseResult = new cc.rengu.igas.channel.upqc.facade.result.Result();
        b2CTranStateQueryResponseResult = (cc.rengu.igas.channel.upqc.facade.result.Result) dubboService.callDubboService(McpsServiceConstant.UPQC_SERVICE, McpsServiceConstant.UPQC_B2CTRANSSTATEQUERY_METHOM, b2CTranStateQueryRequest);
        if (null == b2CTranStateQueryResponseResult || !b2CTranStateQueryResponseResult.isSuccess()) {
            /*调用dubbo失败，默认订单处理中*/
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        B2CTranStateQueryResponse response = b2CTranStateQueryResponseResult.getResult();
        rglog.debug("银联二维码条支付查询相应报文{}", JSON.toJSONString(response));
        /*设置原支付订单相应码和相应信息*/
        oripayOrderInfo.setRespCode(response.getOrigRespCode());
        oripayOrderInfo.setRespDesc(response.getOrigRespDesc());
        /*设置银联返回的付款凭证号*/
        oripayOrderInfo.setChannelOrderId(response.getVoucherNum());
        /*定义订单实现类*/
        OrderService orderService = new OrderServiceImpl();
        MchntService mchntService = new MchntServiceImpl();
        Header tmpHeader = new Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), tmpHeader);
        tmpHeader.setTraceNo(sysSeqNum);
        if ("00000000".equals(response.getRespCode())) {
            if ("00".equals(response.getOrigRespCode())) {
                oripayOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oripayOrderInfo.setRealPayAmt(response.getOrderAmt());
                /*更新原交易相应码*/
                oripayOrderInfo.setRespCode("00000000");
                oripayOrderInfo.setChannelSeqNum(response.getSettleKey());
                oripayOrderInfo.setSettleDate(response.getSettleDate());
                /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                /*成功,处理接受信息，首先判断发卡机构更新付款账户类型*/
                PayerInfo payInfo = response.getPayerInfo();
                /* 获取本行发卡机构号*/
                String issCode = getSysParamIssCode(instId);
                if (null == issCode) {
                    /*没有配置发卡行机构号，用户付款类型默认其他*/
                    /*默认他行卡*/
                    if ("01".equals(payInfo.getCardAttr())) {
                        oripayOrderInfo.setPayerAcctType("10");
                    } else {
                        oripayOrderInfo.setPayerAcctType("11");
                    }

                } else {
                    if ("01".equals(payInfo.getCardAttr())) {
                        /*借记卡，区分本行和他行借记卡*/
                        if (issCode.equals(payInfo.getIssCode())) {
                            oripayOrderInfo.setPayerAcctType("00");
                        } else {
                            oripayOrderInfo.setPayerAcctType("10");
                        }
                    } else {

                        if (issCode.equals(payInfo.getIssCode())) {
                            oripayOrderInfo.setPayerAcctType("01");
                        } else {
                            oripayOrderInfo.setPayerAcctType("11");
                        }
                    }
                }
                /* 判断原订单表中的订单金额是否为空或0.00,若是则对三个订单表中的订单金额进行赋值 */
                if (StringUtil.isEmptyOrNull(oriuserOrderInfo.getOrderAmt()) || AmountUtil.compare(oriuserOrderInfo.getOrderAmt(), "0.00") == 0) {
                    oriuserOrderInfo.setOrderAmt(response.getOrigTxnAmt());
                    oriuserOrderInfo.setNeedPayAmt(response.getOrigTxnAmt());
                }
                if (StringUtil.isEmptyOrNull(orimchntOrderInfo.getOrderAmt()) || AmountUtil.compare(orimchntOrderInfo.getOrderAmt(), "0.00") == 0) {
                    orimchntOrderInfo.setOrderAmt(response.getOrigTxnAmt());
                    orimchntOrderInfo.setRealSettleAmt(response.getOrigTxnAmt());
                }
                if (StringUtil.isEmptyOrNull(oripayOrderInfo.getOrderAmt()) || AmountUtil.compare(oripayOrderInfo.getOrderAmt(), "0.00") == 0) {
                    oripayOrderInfo.setOrderAmt(response.getOrigTxnAmt());
                }
                /* 判断是否参与了银联或者商户营销 */
                if (null != response.getCouponInfoList() && !response.getCouponInfoList().isEmpty()) {
                    for (CouponInfoBean couponInfoBean : response.getCouponInfoList()) {
                        if ("00010000".equals(couponInfoBean.getSpnsrId())) {
                            rglog.info("用户支付享受了银联的营销活动，减免了:{}元", couponInfoBean.getOffstAmt());
                            oripayOrderInfo.setChannelDiscountAmt(couponInfoBean.getOffstAmt());
                        } else if (orimchntOrderInfo.getMchntNo().equals(couponInfoBean.getSpnsrId())) {
                            rglog.info("用户支付享受了商户的营销活动，减免了:{}元", couponInfoBean.getOffstAmt());
                            orimchntOrderInfo.setCustomerMarketAmt(couponInfoBean.getOffstAmt());
                            orimchntOrderInfo.setRealSettleAmt(AmountUtil.subtraction(orimchntOrderInfo.getRealSettleAmt(), orimchntOrderInfo.getCustomerMarketAmt()));
                            oripayOrderInfo.setRealPayAmt(AmountUtil.subtraction(oripayOrderInfo.getRealPayAmt(), couponInfoBean.getOffstAmt()));
                        }
                    }
                    String userPrePayDistAmt = StringUtil.isEmptyOrNull(oriuserOrderInfo.getRealDiscountAmt()) ? "0.00" : oriuserOrderInfo.getRealDiscountAmt();
                    String channelTotalDistAmt = AmountUtil.addition(oripayOrderInfo.getChannelDiscountAmt(), orimchntOrderInfo.getCustomerMarketAmt());
                    oriuserOrderInfo.setRealDiscountAmt(AmountUtil.addition(userPrePayDistAmt, channelTotalDistAmt));
                    oriuserOrderInfo.setRealPayAmt(AmountUtil.subtraction(oriuserOrderInfo.getNeedPayAmt(), channelTotalDistAmt));
                }
                /*需要调用营销，计算手续费跟新商户订单*/
                /*1：计算手续费，查询商户手续费失败*/
                List<MchntFeeInfo> mchntFeeInfoList = mchntService.getMchntFeeInfo(instId, orimchntOrderInfo.getMchntNo());
                if (mchntFeeInfoList.isEmpty()) {
                    rglog.error("查询商户扣率失败交易返回<{}>", orimchntOrderInfo.getMchntNo());
                    throw new BizException(RespCodeEnum.MCHNT_FREE_NOT_EXIST.getRespCode(), RespCodeEnum.MCHNT_FREE_NOT_EXIST.getRespDesc());
                }
                mchntFeeInfoList = mchntFeeInfoList.stream().filter(item -> item.getBaseProdCode().equals(orimchntOrderInfo.getBizProdCode())).collect(Collectors.toList());
                if ("00".equals(payInfo.getCardAttr()) || "10".equals(payInfo.getCardAttr())) {
                    mchntFeeInfoList = mchntFeeInfoList.stream().filter(item -> "03".equals(item.getFeeType())).collect(Collectors.toList());
                } else {
                    mchntFeeInfoList = mchntFeeInfoList.stream().filter(item -> "13".equals(item.getFeeType())).collect(Collectors.toList());
                }
                mchntFeeInfoList = mchntFeeInfoList.stream().filter(item -> item.getFeeEffectiveDate().compareTo(DateUtil.getCurrentDate()) <= 0 && item.getFeeExpiryDate().compareTo(DateUtil.getCurrentDate()) >= 0).collect(Collectors.toList());
                MchntFeeInfo mchntFeeInfo = mchntFeeInfoList.get(0);
                GlobalTransUtil.setCacheValue(TransCacheConstant.MCHNT_FEE_INFO, mchntFeeInfo);
                if (!"0".equals(mchntFeeInfo.getFeeInputMode())) {
                    /*计算商户手续费*/
                    boolean calcResult = mchntService.calcMchntFee(orimchntOrderInfo, mchntFeeInfo.getFeeType());
                    //mod by lizhangtao 由于商户费率信息表中不同的产品存以下三种组合：全特殊费率，全非特殊费率，特殊和非特殊都存在；
                    // 且计算手续费时只要存在特殊费率（不管是不是当前产品的）都不实时计算手续费
                    // 失败：
                    //   1. 返回为空 ||
                    //   2. 该商户计算手续费应答中认为非特殊费率商户（全都是非特殊费率），但是没有手续费
                    // 成功：(除以上失败)
                    //   返回对象非空 && ((返回的FeeInputMode为0 && 无手续费) || (返回的FeeInputMode为非0 && 有手续费))
                    if (!calcResult ||
                            (!"0".equals(orimchntOrderInfo.getFeeInputMode()) &&
                                    null == orimchntOrderInfo.getMchntFee())) {
                        rglog.error("商户手续费计算失败:商户号{},业务产品代码{}", orimchntOrderInfo.getMchntNo(), orimchntOrderInfo.getBizProdCode());
                        throw new BizException(RespCodeEnum.CALC_MCHNT_FEE_ERROR.getRespCode(), RespCodeEnum.CALC_MCHNT_FEE_ERROR.getRespDesc());
                    }
                    if ("0".equals(orimchntOrderInfo.getFeeInputMode())) {
                        rglog.debug("由于存在了其他产品为特殊计费，因此该商户<{}>被认为是特殊费率商户不计算手续费！！", orimchntOrderInfo.getMchntNo());
                        orimchntOrderInfo.setMchntSettleAmt(orimchntOrderInfo.getOrderAmt());
                    }
                } else {
                    rglog.debug("该商户<{}>被认为是特殊费率商户不计算手续费！！", orimchntOrderInfo.getMchntNo());
                    orimchntOrderInfo.setMchntSettleAmt(orimchntOrderInfo.getOrderAmt());
                }
                /*设置商户手续计算方式*/
                orimchntOrderInfo.setFeeInputMode(mchntFeeInfo.getFeeInputMode());
                orimchntOrderInfo.setFeeRate(mchntFeeInfo.getFeeRate());
                orimchntOrderInfo.setFeeCappingAmt(mchntFeeInfo.getFeeCappingAmt());
                orimchntOrderInfo.setFeeFixedAmt(mchntFeeInfo.getFeeFixedAmt());
                oripayOrderInfo.setPayeeAcctInfo(payInfo.getName());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                //更新所有相关的订单表
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                /*保存商户订单表于内部树，用于虚拟账户单笔记账用*/
                xmlTreeUtil.setXmlTreeObjectValue(McpsTreeNodeConstant.MCHNT_ORDER_INFO, orimchntOrderInfo);
                /*
                 * 判断商户是否是实时清算,商户打款实时清算标志，商户是DO清算
                 * */
                MchntAcctInfo mchntAcctInfo = (MchntAcctInfo) xmlTreeUtil.getXmlTreeObjectValue(McpsTreeNodeConstant.MCHNT_ACCT_INFO, MchntAcctInfo.class);
                if (AppParamConstant.YES.equals(orimchntOrderInfo.getRealSettleFlag())) {
                    /*判断清算本金是否小于实时入账最小金额*/
                    String minAmt = getRealTimeClearAmt(orimchntOrderInfo.getInstId());
                    /*判断交易金额是否大于实时入账最低金额*/
                    if (AmountUtil.compare(orimchntOrderInfo.getOrderAmt(), minAmt) != -1) {
                        /*调esb进行D0清算*/
                        EsbRealSttlePaymentService esbRealSttlePaymentService = new EsbRealSttlePaymentServiceImpl();
                        esbRealSttlePaymentService.esbRealSettlePayment(orimchntOrderInfo, oripayOrderInfo, posScanQrCodeStatusQueryRequest.getHeader());
                        orimchntOrderInfo.setRealSettleFlag(AppParamConstant.YES);
                    } else {
                        orimchntOrderInfo.setRealSettleFlag(AppParamConstant.NO);
                    }
                } else {
                    GlobalTransUtil.setCacheValue(TransCacheConstant.USER_ORDER_INFO, oriuserOrderInfo);
                    int iReturnCode = asynnotify.transactionNotice("MchntTransNoticeService", 1, 0, 0);
                    if (0 != iReturnCode) {
                        rglog.error("提交交易结果异步通知给商户失败!");
                    }
                    /*调用微信推送*/
                }
                mchntService.updateMchntTransLimit(orimchntOrderInfo);
                /*TODO  设置相应报文*/
                /*TODO 设置返回报文*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;

            } else if ("04".equals(response.getOrigRespCode()) || "06".equals(response.getOrigRespCode())) {
                /* 处理中交易，支付中状态，返回处理中判断订单有效时间是否超过有效时间，如果超过有效时间，把定单关闭*/
                rglog.debug("<{}>", "<{}>", oriuserOrderInfo.getOrderExpiryTime(), DateUtil.getCurrentDateTime("yyyyMMddHHmmss"));
                if (DateUtil.compareTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"),
                        oriuserOrderInfo.getOrderExpiryTime(), "yyyyMMddHHmmss") > 0) {
                    /*TODO  设置相应报文*/
                    /*更新用户订单、商户订单、支付订单*/
                    oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                    /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                    oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                    oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                    oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    /*商户订单表交易状态 1，业务状态：01 交易完成*/
                    orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                    orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                    List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                    mchntOrderInfoList.add(orimchntOrderInfo);
                    /*更新所有相关的订单表*/
                    orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                } else {
                    /*TODO  设置相应报文*/
                }
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            } else {
                /*失败交易*/
                /*更新用户订单、商户订单、支付订单*/
                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新所有相关的订单表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }

        } else if ("04".equals(response.getRespCode()) || "06".equals(response.getRespCode())) {
            /* 处理中交易，支付中状态，返回处理中判断订单有效时间是否超过有效时间，如果超过有效时间，把定单关闭*/
            if (DateUtil.compareTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"),
                    oriuserOrderInfo.getOrderExpiryTime(), "yyyyMMddHHmmss") > 0) {
                /*TODO  设置相应报文*/
                /*更新用户订单、商户订单、支付订单*/
                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新所有相关的订单表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
            } else {
                /*TODO  设置相应报文*/
            }
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        } else if ("35".equals(response.getRespCode())) {
            /*判断订单有效时间是否超过有效时间，如果超过有效时间，把定单关闭*/
            if (DateUtil.compareTime(DateUtil.getCurrentDateTime("yyyyMMddHHmmss"),
                    oriuserOrderInfo.getOrderExpiryTime(), "yyyyMMddHHmmss") > 0) {
                /* TODO  订单超时定义失败 设置相应报文*/
                /*更新用户订单、商户订单、支付订单*/
                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                /* 用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新所有相关的订单表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
            } else {
            }
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            return bizResponse;
        } else if (response.getRespCode().equals(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode())) {
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        } else if (response.getRespCode().equals(RespCodeEnum.POS_BIZ_SERVICE_ERROR.getRespCode())) {
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(RespCodeEnum.POS_BIZ_SERVICE_ERROR.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.POS_BIZ_SERVICE_ERROR.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        } else {
            /*更新所有相关的订单表*/
            /*更新用户订单、商户订单、支付订单*/
            oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
            oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
            //用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成
            oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
            oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
            oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
            //商户订单表交易状态 1，业务状态：01 交易完成
            orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
            orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
            List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
            mchntOrderInfoList.add(orimchntOrderInfo);
            /*更新所有相关的订单表*/
            orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
    }

    /*微信退款状态查询*/
    BizResponse<PosScanQrCodeStatusQueryResponse> wechatRefundStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                                 UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                                 PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<PosScanQrCodeStatusQueryResponse> bizResponse = new BizResponse<>();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        /*创建相应报文*/
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = new PosScanQrCodeStatusQueryResponse();
        /*机构号、商户号*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        String mchntNo = posScanQrCodeStatusQueryRequest.getMchntNo();
        WechatQuerySingleRefundRequest wechatQuerySingleRefundRequest = new WechatQuerySingleRefundRequest();
        cc.rengu.igas.channel.wechat.facade.base.Header header = new cc.rengu.igas.channel.wechat.facade.base.Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), header);

        wechatQuerySingleRefundRequest.setHeader(header);
        /*设置通道标识*/
        wechatQuerySingleRefundRequest.setChannelId(oripayOrderInfo.getMsgDstId());
        /*设置通道类型*/
        wechatQuerySingleRefundRequest.setChannelType(oripayOrderInfo.getDstChannelType());
        /*获取微信子商户号*/
        if (ChannelEnum.UPWX.getChannelType().equals(wechatQuerySingleRefundRequest.getChannelId())) {
            header.setTxnNum(McpsTxnNumConstant.CUPS_WECHAT_REFUND_QUERY);
        } else {
            header.setTxnNum(McpsTxnNumConstant.WECHAT_REFUND_QUERY);
        }
        /*设置微信子商户号*/
        wechatQuerySingleRefundRequest.setThirdMchntNo(mchntInfo.getWechatMchntNo());
        /*设置退款订单号*/
        wechatQuerySingleRefundRequest.setOrderId(oripayOrderInfo.getOrigSysSeqNum());
        wechatQuerySingleRefundRequest.setRefundOrderId(oripayOrderInfo.getSysSeqNum());
        rglog.debug("微信退款查询请求报文{}", JSON.toJSONString(wechatQuerySingleRefundRequest));
        DubboService dubboService = new DubboServiceImpl();
        /*定义相应报文*/
        cc.rengu.igas.channel.wechat.facade.result.Result<WechatQuerySingleRefundResponse> wechatQuerySingleRefundResponseResult = new cc.rengu.igas.channel.wechat.facade.result.Result();
        wechatQuerySingleRefundResponseResult = (cc.rengu.igas.channel.wechat.facade.result.Result) dubboService.callDubboService(McpsServiceConstant.WECHAT_SERVICE, McpsServiceConstant.WECHAT_QUERYSINGLEREFUND_METHOD, wechatQuerySingleRefundRequest);
        if (null == wechatQuerySingleRefundResponseResult || !wechatQuerySingleRefundResponseResult.isSuccess()) {
            /*调用dubbo失败，默认订单处理中*/
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        WechatQuerySingleRefundResponse response = wechatQuerySingleRefundResponseResult.getResult();
        rglog.debug("微信退款查询相应报文{}", JSON.toJSONString(response));
        /*设置通道退款流水号,通道返的退款响应号*/
        oripayOrderInfo.setChannelOrderId(response.getRefundId());
        /*设置退款状态*/
        oripayOrderInfo.setRespCode(response.getRefundStatus());
        OrderService orderService = new OrderServiceImpl();
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
        PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
        /*退款查证只有明确失败和成功才能调用相关的预授权完成和预授权撤销*/
        Header tmpHeader = new Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), tmpHeader);
        tmpHeader.setTraceNo(sysSeqNum);
        /*获取原支付订单的相关信息*/
        UserOrderInfo oriPayuserOrderInfo = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(instId, oripayOrderInfo.getOrigSysSeqNum());
        if (null == oriPayuserOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        /*获取原商户订单表信息*/
        MchntOrderInfo oriPaymchntOrderInfo = mchntOrderInfoMapper.selectMchntOrderInfoByPrimaryKey(instId, oriPayuserOrderInfo.getSysSeqNum(), mchntNo);
        if (null == oriPaymchntOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        /*获取支付订单列表，获取对应的原交易支付订单*/
        List<PayOrderInfo> payOrderInfoList = payOrderInfoMapper.selectPayOrderInfoBySysSeqNum(instId, oripayOrderInfo.getOrigSysSeqNum());
        if (null == payOrderInfoList || payOrderInfoList.isEmpty()) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        List<PayOrderInfo> oripayOrderInfoList = payOrderInfoList.stream().filter(item -> item.getMsgDstId().equals(ChannelEnum.UPWX.getChannelType()) ||
                item.getMsgDstId().equals(ChannelEnum.UPWX.getChannelType()) && "00".equals(item.getBizStatus()) && "1".equals(item.getTransStatus())).collect(Collectors.toList());
        PayOrderInfo oriPaypayOrderInfo = oripayOrderInfoList.get(0);
        if (null == oriPaypayOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        if ("00000000".equals(response.getRespCode())) {
            if ("SUCCESS".equals(response.getRefundStatus())) {
                /*
                 * 1:更新原支付流水的业务状态
                 * 2:跟新退款订单表中的状态
                 * 3:调用虚拟账户接口预授权完成接口
                 * 设置退款订单的updatetime
                 * */
                oriuserOrderInfo.setUpdateTime(response.getRefundSuccessTime());
                orimchntOrderInfo.setUpdateTime(response.getRefundSuccessTime());
                oripayOrderInfo.setUpdateTime(response.getRefundSuccessTime());
                /*设置退款订单的交易状态*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                /*设置商户订单表*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                /*设置退款支付订单表中的信息*/
                oripayOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oripayOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                if (!"0".equals(orimchntOrderInfo.getFeeInputMode())) {
                    if (AmountUtil.compare(orimchntOrderInfo.getRealSettleAmt(), oriPaymchntOrderInfo.getRealSettleAmt()) == 0) {
                        /*全额退款*/
                        /*退款应退手续*/
                        orimchntOrderInfo.setMchntFee(oriPaymchntOrderInfo.getMchntFee());
                        orimchntOrderInfo.setRealMchntFee(oriPaymchntOrderInfo.getRealMchntFee());
                        orimchntOrderInfo.setMchntDiscountFee(oriPaymchntOrderInfo.getMchntDiscountFee());
                        orimchntOrderInfo.setMchntSettleAmt(oriPaymchntOrderInfo.getMchntSettleAmt());
                    } else {
                        /*计算退款手续费*/
                        MchntService mchntService = new MchntServiceImpl();
                        boolean calcResult = mchntService.calcMchntRefundFee(oriPaymchntOrderInfo, orimchntOrderInfo);
                        if (!calcResult) {
                            rglog.error("商户手续费计算失败:商户号{},业务产品代码{}", oriPaymchntOrderInfo.getMchntNo(), oriPaymchntOrderInfo.getBizProdCode());
                            throw new BizException(RespCodeEnum.CALC_MCHNT_FEE_ERROR.getRespCode(), RespCodeEnum.CALC_MCHNT_FEE_ERROR.getRespDesc());
                        }
                        rglog.debug("手续费计算结果 orimchntOrderInfo:<{}>", JSON.toJSONString(orimchntOrderInfo));
                        rglog.debug("退款应退手续:<{}>", orimchntOrderInfo.getMchntFee());
                        /*商户实际退款手续费*/
                        orimchntOrderInfo.setRealMchntFee(orimchntOrderInfo.getMchntFee());
                        /*商户实际退款金额*/
                        orimchntOrderInfo.setMchntSettleAmt(AmountUtil.subtraction(orimchntOrderInfo.getRealSettleAmt(),
                                orimchntOrderInfo.getRealMchntFee()));
                    }
                } else {
                    orimchntOrderInfo.setMchntSettleAmt(orimchntOrderInfo.getRealSettleAmt());
                }

                if (AmountUtil.compare(orimchntOrderInfo.getRealSettleAmt(), oriPaymchntOrderInfo.getRealSettleAmt()) == 0) {
                    /*全额退款*/
                    /*商户实际退款金额*/
                    orimchntOrderInfo.setOrderAmt(oriPaymchntOrderInfo.getOrderAmt());
                    /*商户退款实际清算本金*/
                    orimchntOrderInfo.setRealSettleAmt(oriPaymchntOrderInfo.getRealSettleAmt());
                    /*商户结算净额*/
                    orimchntOrderInfo.setMchntSettleAmt(oriPaymchntOrderInfo.getMchntSettleAmt());
                    /*商户应收/退手续费*/
                    orimchntOrderInfo.setMchntFee(oriPaymchntOrderInfo.getMchntFee());
                    /*商户实收/退手续费*/
                    orimchntOrderInfo.setRealMchntFee(oriPaymchntOrderInfo.getRealMchntFee());
                    /*商户营销手续*/
                    orimchntOrderInfo.setMchntDiscountFee(oriPaymchntOrderInfo.getMchntDiscountFee());
                    /*原支付交易中记录累计退款金额*/
                    oriPaymchntOrderInfo.setRefundAmt(orimchntOrderInfo.getRealSettleAmt());
                    /*原支付交易中记录累计退款手续费*/
                    oriPaymchntOrderInfo.setRefundFee(orimchntOrderInfo.getRealMchntFee());
                    oriPayuserOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    oriPaypayOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());

                } else {
                    /*部分退款*/
                    /*部分退货，使用营销试算回来的实收手续费，更新部分退货的原商户订单表*/
                    /*累计退款金额=原交易商户订单表中累计退款金额+退款金额*/
                    oriPaymchntOrderInfo.setRefundAmt(AmountUtil.addition(oriPaymchntOrderInfo.getRefundAmt(), orimchntOrderInfo.getRealSettleAmt()));
                    /*累计退款手续费*/
                    oriPaymchntOrderInfo.setRefundFee(AmountUtil.addition(oriPaymchntOrderInfo.getRefundFee(), orimchntOrderInfo.getRealMchntFee()));
                    if (AmountUtil.compare(oriPaymchntOrderInfo.getRefundAmt(), oriPaymchntOrderInfo.getRealSettleAmt()) == 0) {
                        /*更新原交易状态全部退货完成*/
                        rglog.debug("更新原交易状态全部退货完成");
                        oriPayuserOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                        oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                        oriPaypayOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    } else {
                        /*交易状态*/
                        oriPayuserOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                        oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                        oriPaypayOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                    }
                }
                /*更新原订单状态(在这更新有待商榷)*/
                List<MchntOrderInfo> oriPaymchntOrderInfoList = new ArrayList<>();
                oriPaymchntOrderInfoList.add(oriPaymchntOrderInfo);
                GlobalTransUtil.setCacheValue(TransCacheConstant.USER_ORDER_INFO, oriuserOrderInfo);
                int iReturnCode = asynnotify.transactionNotice("MchntTransNoticeService", 1, 0, 0);
                if (0 != iReturnCode) {
                    rglog.error("提交交易结果异步通知给商户失败!");
                }

                /*更新原退款订单*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                /*更新原支付订单*/
                orderService.updateOrderInfo(oriPayuserOrderInfo, oriPaymchntOrderInfoList, oriPaypayOrderInfo);
                MchntService mchntService = new MchntServiceImpl();
                mchntService.updateMchntTransLimit(oriPaymchntOrderInfoList);
                /* TODO 设置返回报文*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            } else if ("PROCESSING".equals(response.getRefundStatus())) {
                /*退款处理中*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            } else {
                /*退款失败,设置退款订单的updatetime*/
                oriuserOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                orimchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                oripayOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                /* 设置退款订单的交易状态*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*设置商户订单表*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*设置退款支付订单表中的信息*/
                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新订单相关的信息表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }
        } else {
            /*返回微信返回错误吗，退款状态不处理，预授权不做处理*/
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(response.getRespCode());
            bizResponse.setRespDesc(response.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
    }

    /*支付宝退款状态查询*/
    BizResponse<PosScanQrCodeStatusQueryResponse> aliPayRefundStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                                 UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                                 PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<PosScanQrCodeStatusQueryResponse> bizResponse = new BizResponse<>();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        /*创建相应报文*/
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = new PosScanQrCodeStatusQueryResponse();
        /*机构号、商户号*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        String mchntNo = posScanQrCodeStatusQueryRequest.getMchntNo();
        AliRefundQueryRequest aliRefundQueryRequest = new AliRefundQueryRequest();
        cc.rengu.igas.channel.alipay.facade.base.Header aliHeader = new cc.rengu.igas.channel.alipay.facade.base.Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), aliHeader);
        aliHeader.setTxnNum(McpsTxnNumConstant.ALIPAY_REFUND_QUERY);
        aliRefundQueryRequest.setHeader(aliHeader);
        /*设置通道标识*/
        aliRefundQueryRequest.setChannelId(oripayOrderInfo.getMsgDstId());
        aliRefundQueryRequest.setChannelType(oripayOrderInfo.getDstChannelType());
        /*获取原支付订单号和退款流水号*/
        aliRefundQueryRequest.setRefundOrderId(oripayOrderInfo.getSysSeqNum());
        /*设置原支付订单号*/
        aliRefundQueryRequest.setOrderId(oripayOrderInfo.getOrigSysSeqNum());
        /*设置网联通道标识*/
        aliRefundQueryRequest.setIdcFlag(oripayOrderInfo.getRemark1());
        aliRefundQueryRequest.setAppAuthToken(mchntInfo.getAlipayAppAuthToken());
        rglog.debug("支付宝退款查询请求报文{}", JSON.toJSONString(aliRefundQueryRequest));
        DubboService dubboService = new DubboServiceImpl();
        cc.rengu.igas.channel.alipay.facade.result.Result<AliRefundQueryResponse> aliRefundQueryResponseResult = new cc.rengu.igas.channel.alipay.facade.result.Result();
        aliRefundQueryResponseResult = (cc.rengu.igas.channel.alipay.facade.result.Result) dubboService.callDubboService(McpsServiceConstant.ALIPAY_SERVICE, McpsServiceConstant.ALIPAY_QUERYREFUND_METHOD, aliRefundQueryRequest);
        if (null == aliRefundQueryResponseResult || !aliRefundQueryResponseResult.isSuccess()) {
            /*调用dubbo失败，默认订单处理中*/
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        AliRefundQueryResponse response = aliRefundQueryResponseResult.getResult();
        rglog.debug("支付宝退款查询相应报文{}", JSON.toJSONString(response));
        oripayOrderInfo.setRespCode(response.getIsFundChange());
        OrderService orderService = new OrderServiceImpl();
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
        PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();

        Header tmpHeader = new Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), tmpHeader);
        tmpHeader.setTraceNo(sysSeqNum);
        /*获取原支付订单的相关信息*/
        UserOrderInfo oriPayuserOrderInfo = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(instId, oripayOrderInfo.getOrigSysSeqNum());
        if (null == oriPayuserOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        /*获取原商户订单表信息*/
        MchntOrderInfo oriPaymchntOrderInfo = mchntOrderInfoMapper.selectMchntOrderInfoByPrimaryKey(instId, oriPayuserOrderInfo.getSysSeqNum(), mchntNo);
        if (null == oriPaymchntOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        /*获取支付订单列表，获取对应的原交易支付订单*/
        List<PayOrderInfo> payOrderInfoList = payOrderInfoMapper.selectPayOrderInfoBySysSeqNum(instId, oripayOrderInfo.getOrigSysSeqNum());
        if (null == payOrderInfoList || payOrderInfoList.isEmpty()) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        List<PayOrderInfo> oripayOrderInfoList = payOrderInfoList.stream().filter(item -> item.getMsgDstId().equals(ChannelEnum.ALIP.getChannelType()) ||
                item.getMsgDstId().equals(ChannelEnum.UPAL.getChannelType()) && "00".equals(item.getBizStatus()) && "1".equals(item.getTransStatus())).collect(Collectors.toList());
        PayOrderInfo oriPaypayOrderInfo = oripayOrderInfoList.get(0);
        if (null == oriPaypayOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        if ("00000000".equals(response.getRespCode())) {
            if ("Y".equals(response.getIsFundChange())) {
                /* 设置退款订单的updatetime*/
                oriuserOrderInfo.setUpdateTime(response.getRefundTime());
                orimchntOrderInfo.setUpdateTime(response.getRefundTime());
                oripayOrderInfo.setUpdateTime(response.getRefundTime());
                /*设置退款订单的交易状态*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                /*设置商户订单表*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                /*设置退款支付订单表中的信息*/
                oripayOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oripayOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<MchntOrderInfo>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*设置原支付订单的更新时间，下档用*/
                oriPayuserOrderInfo.setUpdateTime(response.getRefundTime());
                oriPaymchntOrderInfo.setUpdateTime(response.getRefundTime());
                oriPaypayOrderInfo.setUpdateTime(response.getRefundTime());
                if (!"0".equals(orimchntOrderInfo.getFeeInputMode())) {
                    if (AmountUtil.compare(orimchntOrderInfo.getRealSettleAmt(), oriPaymchntOrderInfo.getRealSettleAmt()) == 0) {
                        /*全额退款*/
                        /*退款应退手续*/
                        orimchntOrderInfo.setMchntFee(oriPaymchntOrderInfo.getMchntFee());
                        orimchntOrderInfo.setRealMchntFee(oriPaymchntOrderInfo.getRealMchntFee());
                        orimchntOrderInfo.setMchntDiscountFee(oriPaymchntOrderInfo.getMchntDiscountFee());
                        orimchntOrderInfo.setMchntSettleAmt(oriPaymchntOrderInfo.getMchntSettleAmt());
                    } else {
                        /*计算退款手续费*/
                        MchntService mchntService = new MchntServiceImpl();
                        boolean calcResult = mchntService.calcMchntRefundFee(oriPaymchntOrderInfo, orimchntOrderInfo);
                        if (!calcResult) {
                            rglog.error("商户手续费计算失败:商户号{},业务产品代码{}", oriPaymchntOrderInfo.getMchntNo(), oriPaymchntOrderInfo.getBizProdCode());
                            throw new BizException(RespCodeEnum.CALC_MCHNT_FEE_ERROR.getRespCode(), RespCodeEnum.CALC_MCHNT_FEE_ERROR.getRespDesc());
                        }
                        rglog.debug("退款应退手续:<{}>", orimchntOrderInfo.getMchntFee());
                        /*商户实际退款手续费*/
                        orimchntOrderInfo.setRealMchntFee(orimchntOrderInfo.getMchntFee());
                        /*商户实际退款金额*/
                        orimchntOrderInfo.setMchntSettleAmt(AmountUtil.subtraction(orimchntOrderInfo.getRealSettleAmt(),
                                orimchntOrderInfo.getRealMchntFee()));
                    }
                } else {
                    orimchntOrderInfo.setMchntSettleAmt(orimchntOrderInfo.getRealSettleAmt());
                }
                if (AmountUtil.compare(orimchntOrderInfo.getRealSettleAmt(), oriPaymchntOrderInfo.getRealSettleAmt()) == 0) {
                    /*全额退款*/
                    /*商户实际退款金额*/
                    orimchntOrderInfo.setOrderAmt(oriPaymchntOrderInfo.getOrderAmt());
                    /*商户退款实际清算本金*/
                    orimchntOrderInfo.setRealSettleAmt(oriPaymchntOrderInfo.getRealSettleAmt());
                    /*商户结算净额*/
                    orimchntOrderInfo.setMchntSettleAmt(oriPaymchntOrderInfo.getMchntSettleAmt());
                    /*商户应收/退手续费*/
                    orimchntOrderInfo.setMchntFee(oriPaymchntOrderInfo.getMchntFee());
                    /*商户实收/退手续费*/
                    orimchntOrderInfo.setRealMchntFee(oriPaymchntOrderInfo.getRealMchntFee());
                    /*商户营销手续*/
                    orimchntOrderInfo.setMchntDiscountFee(oriPaymchntOrderInfo.getMchntDiscountFee());
                    /*原支付交易中记录累计退款金额*/
                    oriPaymchntOrderInfo.setRefundAmt(orimchntOrderInfo.getRealSettleAmt());
                    /*原支付交易中记录累计退款手续费*/
                    oriPaymchntOrderInfo.setRefundFee(orimchntOrderInfo.getRealMchntFee());
                    oriPayuserOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    oriPaypayOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());

                } else {
                    /*部分退款*/
                    /*部分退货，使用营销试算回来的实收手续费，更新部分退货的原商户订单表*/
                    /*累计退款金额=原交易商户订单表中累计退款金额+退款金额*/
                    oriPaymchntOrderInfo.setRefundAmt(AmountUtil.addition(oriPaymchntOrderInfo.getRefundAmt(), orimchntOrderInfo.getRealSettleAmt()));
                    /*累计退款手续费*/
                    oriPaymchntOrderInfo.setRefundFee(AmountUtil.addition(oriPaymchntOrderInfo.getRefundFee(), orimchntOrderInfo.getRealMchntFee()));
                    if (AmountUtil.compare(oriPaymchntOrderInfo.getRefundAmt(), oriPaymchntOrderInfo.getRealSettleAmt()) == 0) {
                        /*更新原交易状态全部退货完成*/
                        rglog.debug("更新原交易状态全部退货完成");
                        oriPayuserOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                        oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                        oriPaypayOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    } else {
                        /*交易状态*/
                        oriPayuserOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                        oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                        oriPaypayOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                    }
                }
                /* 更新原订单状态(在这更新有待商榷)*/
                List<MchntOrderInfo> oriPaymchntOrderInfoList = new ArrayList<>();
                oriPaymchntOrderInfoList.add(oriPaymchntOrderInfo);
                /*获取退款交易时对应的预授权交易*/
                GlobalTransUtil.setCacheValue(TransCacheConstant.USER_ORDER_INFO, oriuserOrderInfo);
                int iReturnCode = asynnotify.transactionNotice("MchntTransNoticeService", 1, 0, 0);
                if (0 != iReturnCode) {
                    rglog.error("提交交易结果异步通知给商户失败!");
                }
                /*更新原退款订单*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                /*更新原支付订单*/
                orderService.updateOrderInfo(oriPayuserOrderInfo, oriPaymchntOrderInfoList, oriPaypayOrderInfo);
                MchntService mchntService = new MchntServiceImpl();
                mchntService.updateMchntTransLimit(oriPaymchntOrderInfoList);
                /* TODO 设置返回报文*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;

            } else {
                /*设置退款订单的updatetime*/
                oriuserOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                orimchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                oripayOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                /*设置退款订单的交易状态*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*设置商户订单表*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*设置退款支付订单表中的信息*/
                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新订单相关的信息表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                /*退款查询中，只有退款成功才会发起营销，所以在查询中退款失败不会做营销的处理*/
                /*scanQrCodeCancelComboPayment(posScanQrCodeStatusQueryRequest);*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }

        } else {
            /*返回支付宝返回错误吗，退款状态不处理，预授权不做处理,继续发起退款查询*/
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
    }

    /*银联二维码退款状态查询*/
    BizResponse<PosScanQrCodeStatusQueryResponse> C2BRefundStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                              UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                              PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        DubboService dubboService = new DubboServiceImpl();
        BizResponse<PosScanQrCodeStatusQueryResponse> bizResponse = new BizResponse<>();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        /*创建相应报文*/
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = new PosScanQrCodeStatusQueryResponse();
        /*机构号、商户号*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        String mchntNo = posScanQrCodeStatusQueryRequest.getMchntNo();
        C2BTranStateQueryRequest c2BTranStateQueryRequest = new C2BTranStateQueryRequest();
        cc.rengu.igas.channel.upqc.facade.base.Header header = new cc.rengu.igas.channel.upqc.facade.base.Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), header);
        header.setTxnNum(McpsTxnNumConstant.C2B_UPQC_QUERY);
        c2BTranStateQueryRequest.setHeader(header);
        if (null == mchntInfo.getCupsInstId()) {
            /*未获取到银联子商户*/
            rglog.error("获取银联收单机构号失败{},{}", instId, orimchntOrderInfo.getMchntNo());
            throw new BizException(RespCodeEnum.UPQC_MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.UPQC_MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        c2BTranStateQueryRequest.setCupsInstCode(mchntInfo.getCupsInstId());
        /*设置查询请求报文*/
        c2BTranStateQueryRequest.setOrderId(oripayOrderInfo.getSysSeqNum());
        c2BTranStateQueryRequest.setOrderTime(oripayOrderInfo.getTxnDate() + oripayOrderInfo.getTxnTime());
        cc.rengu.igas.channel.upqc.facade.result.Result<C2BTranStateQueryResponse> c2BTranStateQueryResponseResult = new cc.rengu.igas.channel.upqc.facade.result.Result();
        rglog.debug("打印请求报文c2BTranStateQueryRequest：<{}>", JSON.toJSONString(c2BTranStateQueryRequest));
        c2BTranStateQueryResponseResult = (cc.rengu.igas.channel.upqc.facade.result.Result) dubboService.callDubboService(McpsServiceConstant.UPQC_SERVICE, McpsServiceConstant.UPQC_C2BTRANSSTATEQUERY_METHOM, c2BTranStateQueryRequest);
        if (null == c2BTranStateQueryResponseResult || !c2BTranStateQueryResponseResult.isSuccess()) {
            /*调用dubbo失败，默认订单处理中*/
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        C2BTranStateQueryResponse response = c2BTranStateQueryResponseResult.getResult();
        /*设置支付订单相应码和相应信息*/
        oripayOrderInfo.setRespCode(response.getOrigRespCode());
        oripayOrderInfo.setRespDesc(response.getOrigRespDesc());
        /*设置银联返回的付款凭证号*/
        oripayOrderInfo.setChannelOrderId(response.getVoucherNum());
        /*定义订单实现类*/
        OrderService orderService = new OrderServiceImpl();
        MchntService mchntService = new MchntServiceImpl();
        Header tmpHeader = new Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), tmpHeader);
        tmpHeader.setTraceNo(sysSeqNum);
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
        PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
        /*获取原支付订单的相关信息*/
        UserOrderInfo oriPayuserOrderInfo = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(instId, oripayOrderInfo.getOrigSysSeqNum());
        if (null == oriPayuserOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        /*获取原商户订单表信息*/
        MchntOrderInfo oriPaymchntOrderInfo = mchntOrderInfoMapper.selectMchntOrderInfoByPrimaryKey(instId, oriPayuserOrderInfo.getSysSeqNum(), mchntNo);
        if (null == oriPaymchntOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        /*获取支付订单列表，获取对应的原交易支付订单*/
        List<PayOrderInfo> payOrderInfoList = payOrderInfoMapper.selectPayOrderInfoBySysSeqNum(instId, oripayOrderInfo.getOrigSysSeqNum());
        if (null == payOrderInfoList || payOrderInfoList.isEmpty()) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        List<PayOrderInfo> oripayOrderInfoList = payOrderInfoList.stream().filter(item -> item.getMsgDstId().equals(ChannelEnum.UPQC.getChannelType()) && "00".equals(item.getBizStatus()) && "1".equals(item.getTransStatus())).collect(Collectors.toList());
        PayOrderInfo oriPaypayOrderInfo = oripayOrderInfoList.get(0);
        if (null == oriPaypayOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        if ("00000000".equals(response.getRespCode())) {
            if ("00".equals(response.getOrigRespCode())) {
                oripayOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                oripayOrderInfo.setChannelSeqNum(response.getSettleKey());
                oripayOrderInfo.setSettleDate(response.getSettleDate());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<MchntOrderInfo>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                if (AmountUtil.compare(orimchntOrderInfo.getRealSettleAmt(), oriPaymchntOrderInfo.getRealSettleAmt()) == 0) {
                    /*全额退款*/
                    /*商户实际退款金额*/
                    orimchntOrderInfo.setOrderAmt(oriPaymchntOrderInfo.getOrderAmt());
                    /*商户退款实际清算本金*/
                    orimchntOrderInfo.setRealSettleAmt(oriPaymchntOrderInfo.getRealSettleAmt());
                    /*商户结算净额*/
                    orimchntOrderInfo.setMchntSettleAmt(oriPaymchntOrderInfo.getMchntSettleAmt());
                    /*商户应收/退手续费*/
                    orimchntOrderInfo.setMchntFee(oriPaymchntOrderInfo.getMchntFee());
                    /*商户实收/退手续费*/
                    orimchntOrderInfo.setRealMchntFee(oriPaymchntOrderInfo.getRealMchntFee());
                    /*商户营销手续*/
                    orimchntOrderInfo.setMchntDiscountFee(oriPaymchntOrderInfo.getMchntDiscountFee());
                    /*原支付交易中记录累计退款金额*/
                    oriPaymchntOrderInfo.setRefundAmt(orimchntOrderInfo.getRealSettleAmt());
                    /*原支付交易中记录累计退款手续费*/
                    oriPaymchntOrderInfo.setRefundFee(orimchntOrderInfo.getRealMchntFee());
                    oriPayuserOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    oriPaypayOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());

                } else {
                    /*部分退款*/
                    /*部分退货，使用营销试算回来的实收手续费，更新部分退货的原商户订单表*/
                    /*累计退款金额=原交易商户订单表中累计退款金额+退款金额*/
                    oriPaymchntOrderInfo.setRefundAmt(AmountUtil.addition(oriPaymchntOrderInfo.getRefundAmt(), orimchntOrderInfo.getRealSettleAmt()));
                    /*累计退款手续费*/
                    oriPaymchntOrderInfo.setRefundFee(AmountUtil.addition(oriPaymchntOrderInfo.getRefundFee(), orimchntOrderInfo.getRealMchntFee()));
                    /*累计退款应收手续费*/
                    if (AmountUtil.compare(oriPaymchntOrderInfo.getRefundAmt(), oriPaymchntOrderInfo.getRealSettleAmt()) == 0) {
                        /*更新原交易状态全部退货完成*/
                        rglog.debug("更新原交易状态全部退货完成");
                        oriPayuserOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                        oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                        oriPaypayOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    } else {
                        /*交易状态*/
                        oriPayuserOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                        oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                        oriPaypayOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                    }
                }
                /* 更新原订单状态(在这更新有待商榷)*/
                List<MchntOrderInfo> oriPaymchntOrderInfoList = new ArrayList<>();
                oriPaymchntOrderInfoList.add(oriPaymchntOrderInfo);
                /*获取退款交易时对应的预授权交易*/
                /*商户特殊费率不调虚拟账户累计*/
                GlobalTransUtil.setCacheValue(TransCacheConstant.USER_ORDER_INFO, oriuserOrderInfo);
                int iReturnCode = asynnotify.transactionNotice("MchntTransNoticeService", 1, 0, 0);
                if (0 != iReturnCode) {
                    rglog.error("提交交易结果异步通知给商户失败!");
                }
                /*更新原退款订单*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                /*更新原支付订单*/
                orderService.updateOrderInfo(oriPayuserOrderInfo, oriPaymchntOrderInfoList, oriPaypayOrderInfo);
                mchntService.updateMchntTransLimit(oriPaymchntOrderInfoList);
                /* TODO 设置返回报文*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            } else {
                /*设置退款订单的updatetime*/
                oriuserOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                orimchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                oripayOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                /*设置退款订单的交易状态*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*设置商户订单表*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*设置退款支付订单表中的信息*/
                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新订单相关的信息表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                /*退款查询中，只有退款成功才会发起营销，所以在查询中退款失败不会做营销的处理*/
                /*scanQrCodeCancelComboPayment(posScanQrCodeStatusQueryRequest);*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }
        } else {
            /*返回支付宝返回错误吗，退款状态不处理，预授权不做处理,继续发起退款查询*/
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
    }

    BizResponse<PosScanQrCodeStatusQueryResponse> B2CRefundStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                              UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                              PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        DubboService dubboService = new DubboServiceImpl();
        BizResponse<PosScanQrCodeStatusQueryResponse> bizResponse = new BizResponse<>();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        /*创建相应报文*/
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = new PosScanQrCodeStatusQueryResponse();
        /*机构号、商户号*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        String mchntNo = posScanQrCodeStatusQueryRequest.getMchntNo();
        B2CTranStateQueryRequest b2CTranStateQueryRequest = new B2CTranStateQueryRequest();
        cc.rengu.igas.channel.upqc.facade.base.Header header = new cc.rengu.igas.channel.upqc.facade.base.Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), header);
        header.setTxnNum(McpsTxnNumConstant.UPQC_B2CCONSUME_REFUND);
        b2CTranStateQueryRequest.setHeader(header);
        if (null == mchntInfo.getCupsInstId()) {
            /*未获取到银联子商户*/
            rglog.error("获取银联收单机构号失败{},{}", instId, orimchntOrderInfo.getMchntNo());
            throw new BizException(RespCodeEnum.UPQC_MCHNT_INFO_NOT_FOUND.getRespCode(), RespCodeEnum.UPQC_MCHNT_INFO_NOT_FOUND.getRespDesc());
        }
        b2CTranStateQueryRequest.setCupsInstCode(mchntInfo.getCupsInstId());
        /*设置查询请求报文*/
        b2CTranStateQueryRequest.setOrderId(oripayOrderInfo.getSysSeqNum());
        b2CTranStateQueryRequest.setOrderTime(oripayOrderInfo.getTxnDate() + oripayOrderInfo.getTxnTime());
        cc.rengu.igas.channel.upqc.facade.result.Result<B2CTranStateQueryResponse> b2CTranStateQueryResponseResult = new cc.rengu.igas.channel.upqc.facade.result.Result();
        rglog.debug("打印请求报文b2CTranStateQueryRequest：<{}>", JSON.toJSONString(b2CTranStateQueryRequest));
        b2CTranStateQueryResponseResult = (cc.rengu.igas.channel.upqc.facade.result.Result) dubboService.callDubboService(McpsServiceConstant.UPQC_SERVICE, McpsServiceConstant.UPQC_B2CTRANSSTATEQUERY_METHOM, b2CTranStateQueryRequest);
        if (null == b2CTranStateQueryResponseResult || !b2CTranStateQueryResponseResult.isSuccess()) {
            /*调用dubbo失败，默认订单处理中*/
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            bizResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
        B2CTranStateQueryResponse response = b2CTranStateQueryResponseResult.getResult();
        /*设置支付订单相应码和相应信息*/
        oripayOrderInfo.setRespCode(response.getOrigRespCode());
        oripayOrderInfo.setRespDesc(response.getOrigRespDesc());
        /*设置银联返回的付款凭证号*/
        oripayOrderInfo.setChannelOrderId(response.getVoucherNum());
        /*定义订单实现类*/
        OrderService orderService = new OrderServiceImpl();
        MchntService mchntService = new MchntServiceImpl();
        Header tmpHeader = new Header();
        BeanUtil.beanCopy(posScanQrCodeStatusQueryRequest.getHeader(), tmpHeader);
        tmpHeader.setTraceNo(sysSeqNum);
        UserOrderInfoMapper userOrderInfoMapper = new UserOrderInfoMapperImpl();
        MchntOrderInfoMapper mchntOrderInfoMapper = new MchntOrderInfoMapperImpl();
        PayOrderInfoMapper payOrderInfoMapper = new PayOrderInfoMapperImpl();
        /*获取原支付订单的相关信息*/
        UserOrderInfo oriPayuserOrderInfo = userOrderInfoMapper.selectUserOrderInfoByPrimaryKey(instId, oripayOrderInfo.getOrigSysSeqNum());
        if (null == oriPayuserOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        /*获取原商户订单表信息*/
        MchntOrderInfo oriPaymchntOrderInfo = mchntOrderInfoMapper.selectMchntOrderInfoByPrimaryKey(instId, oriPayuserOrderInfo.getSysSeqNum(), mchntNo);
        if (null == oriPaymchntOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        /*获取支付订单列表，获取对应的原交易支付订单*/
        List<PayOrderInfo> payOrderInfoList = payOrderInfoMapper.selectPayOrderInfoBySysSeqNum(instId, oripayOrderInfo.getOrigSysSeqNum());
        if (null == payOrderInfoList || payOrderInfoList.isEmpty()) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        List<PayOrderInfo> oripayOrderInfoList = payOrderInfoList.stream().filter(item -> item.getMsgDstId().equals(ChannelEnum.UPWX.getChannelType()) ||
                item.getMsgDstId().equals(ChannelEnum.UPWX.getChannelType()) && "00".equals(item.getBizStatus()) && "1".equals(item.getTransStatus())).collect(Collectors.toList());
        PayOrderInfo oriPaypayOrderInfo = oripayOrderInfoList.get(0);
        if (null == oriPaypayOrderInfo) {
            throw new BizException(RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespCode(), RespCodeEnum.POS_ORIG_TXN_NOT_FOUND.getRespDesc());
        }
        if ("00000000".equals(response.getRespCode())) {
            if ("00".equals(response.getOrigRespCode())) {
                oripayOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                oripayOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                /*用户订单表订单状态：03 订单完成， 交易状态：1 交易成功， 业务状态： 01交易完成*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                oripayOrderInfo.setChannelSeqNum(response.getSettleKey());
                oripayOrderInfo.setSettleDate(response.getSettleDate());
                /*商户订单表交易状态 1，业务状态：01 交易完成*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.TRANS_SUCC.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FINISH.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<MchntOrderInfo>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                if (AmountUtil.compare(orimchntOrderInfo.getRealSettleAmt(), oriPaymchntOrderInfo.getRealSettleAmt()) == 0) {
                    /*全额退款*/
                    /*商户实际退款金额*/
                    orimchntOrderInfo.setOrderAmt(oriPaymchntOrderInfo.getOrderAmt());
                    /*商户退款实际清算本金*/
                    orimchntOrderInfo.setRealSettleAmt(oriPaymchntOrderInfo.getRealSettleAmt());
                    /*商户结算净额*/
                    orimchntOrderInfo.setMchntSettleAmt(oriPaymchntOrderInfo.getMchntSettleAmt());
                    /*商户应收/退手续费*/
                    orimchntOrderInfo.setMchntFee(oriPaymchntOrderInfo.getMchntFee());
                    /*商户实收/退手续费*/
                    orimchntOrderInfo.setRealMchntFee(oriPaymchntOrderInfo.getRealMchntFee());
                    /*商户营销手续*/
                    orimchntOrderInfo.setMchntDiscountFee(oriPaymchntOrderInfo.getMchntDiscountFee());
                    /*原支付交易中记录累计退款金额*/
                    oriPaymchntOrderInfo.setRefundAmt(orimchntOrderInfo.getRealSettleAmt());
                    /*原支付交易中记录累计退款手续费*/
                    oriPaymchntOrderInfo.setRefundFee(orimchntOrderInfo.getRealMchntFee());
                    oriPayuserOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    oriPaypayOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());

                } else {
                    /*部分退款*/
                    /*部分退货，使用营销试算回来的实收手续费，更新部分退货的原商户订单表*/
                    /*累计退款金额=原交易商户订单表中累计退款金额+退款金额*/
                    oriPaymchntOrderInfo.setRefundAmt(AmountUtil.addition(oriPaymchntOrderInfo.getRefundAmt(), orimchntOrderInfo.getRealSettleAmt()));
                    /*累计退款手续费*/
                    oriPaymchntOrderInfo.setRefundFee(AmountUtil.addition(oriPaymchntOrderInfo.getRefundFee(), orimchntOrderInfo.getRealMchntFee()));
                    /*累计退款应收手续费*/
                    if (AmountUtil.compare(oriPaymchntOrderInfo.getRefundAmt(), oriPaymchntOrderInfo.getRealSettleAmt()) == 0) {
                        /*更新原交易状态全部退货完成*/
                        rglog.debug("更新原交易状态全部退货完成");
                        oriPayuserOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                        oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                        oriPaypayOrderInfo.setBizStatus(BizStatusEnum.REFUND.getStatus());
                    } else {
                        /*交易状态*/
                        oriPayuserOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                        oriPaymchntOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                        oriPaypayOrderInfo.setBizStatus(BizStatusEnum.PART_REFUND.getStatus());
                    }
                }
                /* 更新原订单状态(在这更新有待商榷)*/
                List<MchntOrderInfo> oriPaymchntOrderInfoList = new ArrayList<>();
                oriPaymchntOrderInfoList.add(oriPaymchntOrderInfo);
                /*获取退款交易时对应的预授权交易*/
                GlobalTransUtil.setCacheValue(TransCacheConstant.USER_ORDER_INFO, oriuserOrderInfo);
                int iReturnCode = asynnotify.transactionNotice("MchntTransNoticeService", 1, 0, 0);
                if (0 != iReturnCode) {
                    rglog.error("提交交易结果异步通知给商户失败!");
                }
                /*更新原退款订单*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                /*更新原支付订单*/
                orderService.updateOrderInfo(oriPayuserOrderInfo, oriPaymchntOrderInfoList, oriPaypayOrderInfo);
                mchntService.updateMchntTransLimit(oriPaymchntOrderInfoList);
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                /* TODO 设置返回报文*/
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            } else {
                /*设置退款订单的updatetime*/
                oriuserOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                orimchntOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                oripayOrderInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
                /*设置退款订单的交易状态*/
                oriuserOrderInfo.setOrderStatus(OrderStatusEnum.FINISH.getStatus());
                oriuserOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oriuserOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*设置商户订单表*/
                orimchntOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                orimchntOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                /*设置退款支付订单表中的信息*/
                oripayOrderInfo.setTransStatus(TransStatusEnum.SYSTEM_REJECT.getStatus());
                oripayOrderInfo.setBizStatus(BizStatusEnum.FAILED.getStatus());
                List<MchntOrderInfo> mchntOrderInfoList = new ArrayList<>();
                mchntOrderInfoList.add(orimchntOrderInfo);
                /*更新订单相关的信息表*/
                orderService.updateOrderInfo(oriuserOrderInfo, mchntOrderInfoList, oripayOrderInfo);
                /*退款查询中，只有退款成功才会发起营销，所以在查询中退款失败不会做营销的处理*/
                /*scanQrCodeCancelComboPayment(posScanQrCodeStatusQueryRequest);*/
                OrderInfoBean orderInfo = new OrderInfoBean();
                PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
                orderInfo.setPayTypeInfo(payTypeInfo);
                BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
                BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
                posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
                bizResponse.setResult(posScanQrCodeStatusQueryResponse);
                bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
                bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
                bizResponse.setRspSysId(AppParamConstant.SYS_ID);
                return bizResponse;
            }
        } else {
            /*返回支付宝返回错误吗，退款状态不处理，预授权不做处理,继续发起退款查询*/
            OrderInfoBean orderInfo = new OrderInfoBean();
            PayTypeInfo payTypeInfo = convPayTypeInfo(oripayOrderInfo);
            orderInfo.setPayTypeInfo(payTypeInfo);
            BeanUtil.beanCopy(oripayOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(oriuserOrderInfo, orderInfo);
            BeanUtil.nullValueCopy(orimchntOrderInfo, orderInfo);
            posScanQrCodeStatusQueryResponse.setOrderInfo(orderInfo);
            bizResponse.setResult(posScanQrCodeStatusQueryResponse);
            bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            return bizResponse;
        }
    }

    /*本行借记卡快捷查询*/
    BizResponse<PosScanQrCodeStatusQueryResponse> esbDebitScanQrCodeStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                                       UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                                       PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<PosScanQrCodeStatusQueryResponse> bizResponse = new BizResponse<>();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        /*创建相应报文*/
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = new PosScanQrCodeStatusQueryResponse();
        /*机构号、商户号*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        String mchntNo = posScanQrCodeStatusQueryRequest.getMchntNo();

        /* 调用esb核心查询接口 */
        // TODO 交易状态查询
        // （1） 查询失败/超时，返回失败
        // (2) 查询成功，判断业务状态，成功：更新原订单为成功，判断是否调用商户是DO清算或调虚拟账户记账，累计商户限额 mchntService.addTxnLimitStatis
        //                           失败：更新原订单为失败，调用营销冲正comboDealService.cancelComboPayment

        return bizResponse;
    }

    /*本行贷记卡快捷查询*/
    BizResponse<PosScanQrCodeStatusQueryResponse> esbCreditScanQrCodeStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                                        UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                                        PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        BizResponse<PosScanQrCodeStatusQueryResponse> bizResponse = new BizResponse<>();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(McpsTreeNodeConstant.SYS_SEQ_NUM);
        /*创建相应报文*/
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = new PosScanQrCodeStatusQueryResponse();
        /*机构号、商户号*/
        String instId = posScanQrCodeStatusQueryRequest.getHeader().getInstId();
        String mchntNo = posScanQrCodeStatusQueryRequest.getMchntNo();

        /* 调用esb核心查询接口 */
        // TODO 交易状态查询
        // （1） 查询失败/超时，返回失败
        // (2) 查询成功，判断业务状态，成功：更新订单为成功，判断是否调用商户是DO清算或调虚拟账户记账，累计商户限额 mchntService.addTxnLimitStatis
        //                           失败：更新订单为失败，调用营销冲正comboDealService.cancelComboPayment
        return bizResponse;
    }

    BizResponse<PosScanQrCodeStatusQueryResponse> esbRefundStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                              UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                              PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        /*退款查证暂时未实现*/
        return null;

    }

    BizResponse<PosScanQrCodeStatusQueryResponse> upacRefundStatusQueryService(PosScanQrCodeStatusQueryRequest posScanQrCodeStatusQueryRequest,
                                                                               UserOrderInfo oriuserOrderInfo, MchntOrderInfo orimchntOrderInfo,
                                                                               PayOrderInfo oripayOrderInfo, MchntInfo mchntInfo) throws Exception {
        /*退款查证暂时未实现*/
        return null;

    }

    private String getSysParamCardType(String instId, String paramType, String cardType) throws Exception {
        String sCardType = null;
        //key组成:instId+
        String paramKey = paramType + cardType;
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(instId, paramType, paramKey);
        if (null != sysParam && null != sysParam.getParamValue()) {
            sCardType = sysParam.getParamValue();
        }
        return sCardType;
    }

    private String getSysParamIssCode(String instId) throws Exception {
        String issCode = null;
        /* 查询系统参数表*/
        String paramType = McpsParamConstant.NORMAL_CARDTYPE_UPQC;
        /* key组成:instId+paramvalue+paramkey*/
        String paramKey = McpsParamConstant.NORMAL_CARDTYPE_UPQC + "issCode";
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(instId, paramType, paramKey);
        if (null != sysParam && null != sysParam.getParamValue()) {
            issCode = sysParam.getParamValue();
        }
        return issCode;
    }

    /*获取实时入账最小金额*/
    public String getRealTimeClearAmt(String instId) throws Exception {
        String paramType = "MCHNT_SERVICE";
        String paramKey = "D0_ACCOUNTING_MIN_AMT";
        SysParamService sysParamService = new SysParamServiceImpl();
        SysParam sysParam = sysParamService.getSysParamInfo(instId, paramType, paramKey);
        if (null == sysParam) {
            rglog.info("实时入账最小金额未配置,使用默认金额");
            return McpsParamConstant.DEFAULT_REAL_TIME_PAY_MIN_AMT;
        } else {
            return sysParam.getParamValue();
        }
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        PosScanQrCodeStatusQueryResponse posScanQrCodeStatusQueryResponse = (PosScanQrCodeStatusQueryResponse) bizResponse.getResult();
        posScanQrCodeStatusQueryResponse.setRespCode(bizResponse.getRespCode());
        posScanQrCodeStatusQueryResponse.setRespDesc(bizResponse.getRespDesc());
        ConvertUtil.convertInput(posScanQrCodeStatusQueryResponse);
    }

    /*获取商户结算账户信息*/
    private MchntAcctInfo getMchntAcctInfo(PosScanQrCodeStatusQueryRequest request, MchntInfo mchntInfo) throws Exception {
        String mchntNo = mchntInfo.getMchntNo();
        /*判断商户是否为独立清算商户*/
        if (!"Y".equals(mchntInfo.getIndependentDataFlag()) && "11".equals(mchntInfo.getMchntType())) {
            mchntNo = mchntInfo.getChainMchntNo();
        }
        MchntService mchntService = new MchntServiceImpl();
        List<MchntAcctInfo> mchntAcctInfoList = mchntService.getMchntAcctInfo(request.getHeader().getInstId(), mchntNo);
        Optional<MchntAcctInfo> mchntAcctInfoOpt = mchntAcctInfoList.stream().filter(item -> item.getFundType().equals("00")).findFirst();
        return mchntAcctInfoOpt.orElse(null);
    }

    /**
     * 转换接口返回订单明细-订单支付方式
     *
     * @param payOrderInfo 支付订单信息
     * @return 订单支付方式
     * @throws Exception 异常
     */
    private PayTypeInfo convPayTypeInfo(PayOrderInfo payOrderInfo) throws Exception {
        if (null != payOrderInfo) {
            PayTypeInfo payAcctInfoBean = new PayTypeInfo();
            payAcctInfoBean.setPayType(payOrderInfo.getPayType());
            payAcctInfoBean.setTranAmt(payOrderInfo.getRealPayAmt());
            payAcctInfoBean.setPayerAcctInfo(payOrderInfo.getPayerAcctInfo());
            payAcctInfoBean.setPayerAcctType(payOrderInfo.getPayerAcctType());
            return payAcctInfoBean;
        }
        return null;
    }
}